Vogliamo realizzare uno script python che legga una pagina html contenente una tabella standard e ne restituisca una versione JSON riutilizzabile ed esponibile ad altri servizi web oriented.
from bs4 import BeautifulSoup
import requests
import json
for docente in range(0,104):
url="http://www.iisteramo.edu.it/informazioni/orario_itis/2020_21/settimana_4/Docenti/Docente"+str(docente)+".html"
# Preleva il flusso HTML grezzo
html_content = requests.get(url).text
# Parse html
soup = BeautifulSoup(html_content, "lxml")
table = soup.find("table")
tr = table.find_all("tr")
giorni = []
primo = []
secondo = []
terzo = []
quarto = []
quinto = []
sesto = []
settimo = []
orario = []
for td in tr[0].find_all("td"):
giorni.append(td.text.strip())
for td in tr[1].find_all("td"):
primo.append(td.text.strip())
for td in tr[2].find_all("td"):
secondo.append(td.text.strip())
for td in tr[3].find_all("td"):
terzo.append(td.text.strip())
for td in tr[4].find_all("td"):
quarto.append(td.text.strip())
for td in tr[5].find_all("td"):
quinto.append(td.text.strip())
for td in tr[6].find_all("td"):
sesto.append(td.text.strip())
for td in tr[7].find_all("td"):
settimo.append(td.text.strip())
data = {0:giorni, 1:primo, 2:secondo, 3:terzo, 4:quarto, 5:quinto, 6:sesto, 7:settimo}
file = "json"+str(docente)+".json"
f = open(file,'w')
json.dump(data, f)
f.close()
Ultima modifica 26 Maggio 2022