con request e selenium invece di salvare tutta la pagina html dobbiamo solo salvare…
- User
con request e selenium invece di salvare tutta la pagina html dobbiamo solo salvare queste parti:
code - Assistant
con request e selenium invece di salvare tutta la pagina html dobbiamo solo salvare queste parti:
Versamento
acquisito nell'anno 2017
effettuato in data 3/4/2017
</h3>
<!-- intestazione del versamento -->
<div class="card">
<div class="card-body">
<p>
Protocollo
<b>
17040319314454217/000002
</b>
data del versamento <b>3/4/2017</b>
<br>
Intermediario
<b>
<span class="correlato">TPEGNN66L29C286Z<button type="button" title="Dati del soggetto TPEGNN66L29C286Z" value="TPEGNN66L29C286Z" class="btnCorrelato btn btn-alt btn-sm ml-1" data-toggle="modal" data-target="#modal-correlato"><i class="fas fa-user-circle fa-lg icon-primary"></i><span class="sr-only">Dati del soggetto TPEGNN66L29C286Z [I dati verranno presentati in una finestra modale]</span></button></span>
</b>
codice ABI <b>02008</b>
codice CAB <b>81830</b>
<br><br>
Codice ufficio <b>TXM</b>
codice atto <b>12995804007</b>
</p>
</div>
</div>
<!-- dati di dettaglio -->
<div class="border border-info px-2 py-2 mb-3">
<h4>
Sezione ERARIO
</h4>
<div class="table-responsive">
<table class="table table-hover">
<caption class="sr-only">La tabella riporta i dati di dettaglio della Sezione ERARIO del versamento</caption>
<thead>
<tr>
<!-- riga intestazione -->
<th colspan="3" id="tributo">Tributo</th>
<th id="rateazione">Rateazione, regione/provincia, mese rif.</th>
<th id="anno">Anno di riferimento</th>
<th id="debito">Importo a debito <br>versato</th>
<th id="credito">Importo a credito <br>compensato</th>
</tr>
</thead>
<tbody>
<tr>
<!-- prima riga dettaglio -->
<th headers="tributo">
9451
</th>
<td colspan="2" headers="tributo">
IRPEF E RELATIVI INTERESSI OMESSA IMPUGNAZIONE
</td>
<td headers="rateazione">
</td>
<td headers="anno">
2011
</td>
<td class="text-right text-primary" headers="debito">
109,42 €
</td>
<td class="text-right text-primary" headers="credito">
0,00 €
</td>
</tr>
<!-- seconda riga dettaglio puntuale-->
<tr>
<!-- prima riga dettaglio -->
<th headers="tributo">
9452
</th>
<td colspan="2" headers="tributo">
SANZIONE E ALTRE SOMME DOVUTE RELATIVE AI TRIBUTI ERARIALI OMESSA IMPUGNAZIONE
</td>
<td headers="rateazione">
</td>
<td headers="anno">
2011
</td>
<td class="text-right text-primary" headers="debito">
19,55 €
</td>
<td class="text-right text-primary" headers="credito">
0,00 €
</td>
</tr>
<!-- seconda riga dettaglio puntuale-->
<tr>
<!-- prima riga dettaglio -->
<th headers="tributo">
9454
</th>
<td colspan="2" headers="tributo">
SANZIONE E ALTRE SOMME DOVUTE RELATIVE ALL'ADDIZIONALE REGIONALE ALL'IRPEF OMES
</td>
<td headers="rateazione">
16
</td>
<td headers="anno">
2011
</td>
<td class="text-right text-primary" headers="debito">
5,41 €
</td>
<td class="text-right text-primary" headers="credito">
0,00 €
</td>
</tr>
<!-- seconda riga dettaglio puntuale-->
<tr>
<!-- prima riga dettaglio -->
<th headers="tributo">
9453
</th>
<td colspan="2" headers="tributo">
ADDIZIONALE REGIONALE ALL'IRPEF E RELATIVI INTERESSI OMESSA IMPUGNAZIONE
</td>
<td headers="rateazione">
16
</td>
<td headers="anno">
2011
</td>
<td class="text-right text-primary" headers="debito">
5,20 €
</td>
<td class="text-right text-primary" headers="credito">
0,00 €
</td>
</tr>
<!-- seconda riga dettaglio puntuale-->
</tbody>
<tfoot>
<tr>
<th scope="row" colspan="5" class="text-right text-primary">Totali</th>
<td class="text-right text-primary">
<strong>139,58 €</strong>
</td>
<td class="text-right text-primary">
<strong>0,00 €</strong>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="border border-info px-2 py-2 mb-3">
<h4>
<em>Totale versato
</em>
</h4>
<p class="text-right text-primary">
<strong>Versamento totale 139,58 €</strong>
</p>
</div>
</div>
Per soddisfare la tua richiesta utilizzando requests e lxml in Python, puoi eseguire la seguente operazione per catturare e salvare solo le parti specifiche della pagina HTML che desideri:
import requests
from lxml import html
# Perform an HTTP GET request to the page where your data is located
url = 'https://example.com' # Replace this with the actual URL
response = requests.get(url)
# Parse the HTML content
tree = html.fromstring(response.content)
# Specify the XPath to extract the desired parts of the HTML
elements_to_extract = tree.xpath('//div[@id="print"]')
# Convert the extracted elements back to a string
extracted_html_content = ''.join([html.tostring(element, encoding='unicode', pretty_print=True) for element in elements_to_extract])
# Save the extracted HTML to a file
with open('extracted_content.html', 'w', encoding='utf-8') as file:
file.write(extracted_html_content)
Assicurati di sostituire 'https://example.com' con l'URL effettivo della pagina web da cui vuoi estrarre il contenuto. Inoltre, se il contenuto richiesto è generato tramite JavaScript, requests e lxml non saranno sufficienti, poiché requests non esegue JavaScript. In tal caso, dovresti utilizzare selenium per eseguire il rendering della pagina prima dell'estrazione.
Per eseguire un'azione analoga usando selenium, puoi seguire questi passi:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Configure the WebDriver (Chrome, in this example)
driver = webdriver.Chrome(executable_path='path/to/your/chromedriver')
# Open the page
driver.get('https://example.com') # Replace this with the actual URL
# Wait for necessary JavaScript to execute and render (if needed)
# You might want to use WebDriverWait and expected_conditions here
# Use Selenium's capabilities to find the element
element = driver.find_element(By.ID, "print")
# Extract the HTML of the desired element
extracted_html_content = element.get_attribute('outerHTML')
# Close the WebDriver
driver.quit()
# Save the extracted HTML to a file
with open('extracted_content.html', 'w', encoding='utf-8') as file:
file.write(extracted_html_content)
Ancora una volta, sostituisci 'https://example.com' e 'path/to/your/chromedriver' con l'URL della pagina e il percorso del tuo WebDriver.
Entrambi gli esempi sopra descritti presuppongono che tu stia cercando di isolare un particolare div con id="print" e voglia estrarre il suo contenuto con tutto lo HTML al suo interno. Nel tuo xpath puoi specificare ulteriori criteri per restringere l'estrazione alle parti esatte che ti servono.