NEW UPDATE This is the main window. It consists of displaying a green icon (scraping performed correctly) or a red icon (scraping error) next to each text, based on the outcome of the scraping. Then basically, there is an icon with a question mark that is replaced by the red or green icon. In addition to the icon change, the error message is also displayed (the error messages with the exception are contained in another external file, which I use for scraping). Scraping is performed thanks to two py scripts (or rather thanks to their functions) that are imported. They are msg1 = Scraping_Nome_Campionati.scraping_nome_campionati_e_tor, and msg2 = Scraping_Nome_Squadre_MIO.scraping_nome_squadre_e_tor()
def do_scraping():
msg1 = Scraping_Nome_Campionati.scraping_nome_campionati_e_tor()
if msg1:
message1.configure(text=msg1)
message1.configure(foreground="red")
vuoto_elenco_campionati.config(image=render7)
else:
vuoto_elenco_campionati.config(image=render8)
message1.configure(foreground="green")
msg2 = Scraping_Nome_Squadre_MIO.scraping_nome_squadre_e_tor()
if msg2:
message2.configure(text=msg2)
message2.configure(foreground="red")
vuoto_elenco_squadre.config(image=render7)
else:
vuoto_elenco_squadre.config(image=render8)
message2.configure(foreground="green")
button = Button(test_scraping, text="Avvia", bg='#e95420', foreground='white', command=do_scraping)
button.place(x=116, y=512)
The scraping in the two external files is performed like this (I carry only 1 file for example purposes)
#example
driver.minimize_window()
driver.get("web site")
Element1=driver.find_element_by_class_name("name class")
Element1_text = Element1.text
#insert in database
con = sqlite3.connect('/dababase')
cursor = con.cursor()
records_added_Risultati = 0
Values = ((Element1_text,), (Element2_text,))
sqlite_insert_query = 'INSERT INTO xxxxx VALUES (?);'
count = cursor.executemany(sqlite_insert_query, Values)
con.commit()
#The error messages are contained in this external scraping file,
# and NOT in the main window with icons
except NoSuchElementException:
return "FAILED (NoSuchElementException)"
except NameError:
return "FAILED (Name Error)"
except ValueError:
return "FAILED (ValueError)"
if records_added_Risultati == 0:
return "FAILED: 0 record scraping"
Campionati_per_controllo_errori = [Element1_text, Element2_text]
if any(Campionati_per_controllo_errori):
return "FAILED (manca 1 campionato)"
PROBLEM: So to recap, the problem is that when scraping is done correctly (both scraping and database inserting), I still get the red error icon + error message. I set a type of error that makes sure that "if a scraping, even 1 only, is not performed correctly, the error must be printed", that is, if 19 of 20 texts are downloaded correctly, an error must appear and its message. The problem is that: even if you scrape 20 texts and everything is done correctly, I get the message that there is an error and a text has not been scraped.
OLD UPDATE
How can I code the following:
"If one or more" of these elements is not detected, then I get a warning message. By warning message, I mean in case there is a scraping problem that results in, NOT an error, but just an empty result. So that one Element is not scraped, but the others following do.
If all are not detected, then do I display a warning message?
So I would need 2 types of code.
#example
driver.minimize_window()
driver.get("web site")
Element1=driver.find_element_by_class_name("name class")
Element1 = Element1text
if Element1 or Element2 or Element3 or Element4 or Element5 = no scraping:
return "error"
if Element1, Element2, Element3, Element4, Element5 = no scraping:
return "error"
UPDATE Elements are data scraped with Selenium and saved in a database. For example they are like this:
#Element1
driver.get("site")
for Element1 in driver.find_elements(By.CSS_SELECTOR, xxxxxx'][class^='rxxxxxx']"):
Element1_text = Element1.text
count = cursor.execute(sqlite_insert_query, (Element1_text,))
print(Element1_text)
driver.close
#insert in database
con = sqlite3.connect('/dababase')
cursor = con.cursor()
records_added_Risultati = 0
Values = ((Element1_text,), (Element2_text,))
sqlite_insert_query = 'INSERT INTO xxxxx VALUES (?);'
count = cursor.executemany(sqlite_insert_query, Values)
con.commit()