con questo codice ho problemi con i multi f24, in buona sostanza qualora nella lista si riscontra che i modelli sono più di uno, si deve aprire il relativo link in cui compare nuovamente una nuova tabella in cui si devono ciclare i relativi link e salvare i file
def anni(driver):
html_source = driver.page_source
# Close the browser (if needed)
# driver.quit()
# Parse the HTML using BeautifulSoup
soup = BeautifulSoup(html_source, 'html.parser')
base_url = 'https://cassetto.agenziaentrate.gov.it'
# Find all the years within the specified section
section_years = soup.find('section', class_='section section-anni border border-primary')
years = []
if section_years:
# Find all the span elements with class 'chip-label' within the section
year_elements = section_years.find_all('span', class_='chip-label')
# Extract the text content (years) from the elements
years = [element.get_text(strip=True) for element in year_elements]
# Print the extracted years
#print(years)
html_source = driver.page_source
# Close the browser (if needed)
# driver.quit()
# Parse the HTML using BeautifulSoup
soup = BeautifulSoup(html_source, 'html.parser')
# Find all the years within the specified section
documenti_presenti = soup.select('.chip .btn-sm a[title="Documenti presenti"]')
# Creazione di un dizionario con gli anni e i link
data = {}
anni=[]
for elem in documenti_presenti:
anno = elem.find(class_='chip-label').get_text(strip=True)
link = base_url + elem['href']
data[anno] = link
anni.append({anno: link})
# Stampa del risultato
for anno_dict in anni:
for anno, link in anno_dict.items():
print(f'Anno: {anno}, Link: {link}')
print(anno)
print(link)
driver.get(link)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "table.table-hover tbody tr")))
lista_f24(driver,anno)
contatore = 0
def lista_f24(driver,anno, num_f24=None, multiplo=None):
global contatore
# Trova tutti i righe () all'interno del corpo della tabella ()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "table.table-hover tbody tr")))
rows = driver.find_elements(By.CSS_SELECTOR, "table.table-hover tbody tr")
# Inizializza una lista per i dati e i link da archiviare
f24_data = []
# Andiamo a estrarre le informazioni da ogni riga
for row in rows:
# Ottieni tutti i dati delle colonne e mettiteli in una lista
data = [cell.text.strip() for cell in row.find_elements(By.TAG_NAME, "th")] + [cell.text.strip() for cell in row.find_elements(By.TAG_NAME, "td")] + [a.get_attribute('href') for a in row.find_elements(By.TAG_NAME, "a")]
# Aggiungi i dati e i link alla lista
f24_data.append({'data': data})
#print(data)
text =""
testo_protocollo=""
for item in f24_data:
if contatore == 0:
num_f24= int(item['data'][2])
print(' numero f24 da lista ' + str(num_f24))
else:
num_f24
print(' numero f24 da multi f24 ' + str(num_f24))
text = str(item['data'][1])
testo_protocollo=item['data'][4]
print('inizializzaione link')
text = str(item['data'][1])
print("Numero f24_________________ " + str(num_f24))
print(text)
if "dettaglio del versamento" in text:
# Estrai il numero del versamento e l'indice
versamento = text.split('n.')[0].strip()
indice = str(text.split('n.')[1].split(' ')[0].strip())
# Stampa il numero del versamento e l'indice
print('Versamento:', versamento)
print('Indice:', indice)
# Ottieni il testo del protocollo corrispondente
protocollo = anno + '_'+ testo_protocollo.replace('/', '_') + '_' + indice
print(protocollo)
if num_f24 ==1 and multiplo != "multiplo":
if '-' in testo_protocollo:
href= str(item['data'][6])
print('link da eseguire ' + href)
f24_singolo(driver,protocollo,text, href,indice)
else:
href= str(item['data'][7])
print('link da eseguire ' + href)
f24_singolo(driver,protocollo,text, href,indice)
breakpoint()
# else:
if contatore == 0:
driver.get(href)
lista_f24(driver,anno,num_f24,"multiplo")
print('multi f24')
contatore += 1
num_f24=int(contatore)
print (num_f24)
print(item)
href= str(item['data'][6])
print('Link f24 lista')
print('link da eseguire ' + href)
f24_singolo(driver, protocollo, text, href, indice)
breakpoint()
else:
#f24_multipli(driver,anno,href,f24_num, protocollo, text, indice)
print('multi f24')
contatore += 1
num_f24=int(contatore)
print (num_f24)
print(item)
href= str(item['data'][6])
print('Link f24 lista')
print('link da eseguire ' + href)
if contatore != 0:
f24_singolo(driver, protocollo, text, href, indice)
breakpoint()
def f24_singolo(driver, protocollo, text, href, indice):
print("download pagina html")
file_name = f"{protocollo}.html"
file_path = os.path.join('C:/Users/Utente/Desktop/Cassetto fiscale', file_name)
# Crea una sessione di requests e trasferisci i cookie
session = requests.Session()
cookies = driver.get_cookies()
for cookie in cookies:
session.cookies.set(cookie['name'], cookie['value'])
# Salva la pagina HTML
response = session.get(href)
with open(file_path, 'wb') as f:
f.write(response.content)
print('html '+ file_name + ' salvato')
# Fa il download degli f24 singoli
for suffix in ['P', 'Q']:
pdf_file_name = f"{protocollo}_{suffix}.pdf"
pdf_file_path = os.path.join('C:/Users/Utente/Desktop/Cassetto fiscale', pdf_file_name)
url_suffix = f"{href}&stampa={suffix}"
# Salva i file PDF
#response = session.get(url_suffix)
file_bytes = session.get(url_suffix).content
with open(pdf_file_name, "wb") as f:
f.write(file_bytes)
print('download file ' + pdf_file_name)
#def f24_multipli(driver,anno,href,f24_num, protocollo, text, indice):
#f24_singolo(driver, protocollo, text, href, indice)
if name == "main":
main()