Ich möchte Parsen die Afk., Aantal und Zetels Spalten auf der Website: http://www.nlverkiezingen.com/TK2012.html, dass ich schließlich als JSON-Datei speichern kann.Parsing Spalten mit BeautifulSoup und Speichern als JSON
Bevor Sie es als JSON-Datei speichern, muss ich die Elemente analysieren.
hatte ich
from bs4 import BeautifulSoup
import urllib
jaren = [str("2010"), str("2012")]
for Jaargetal in jaren:
r = urllib.urlopen("http://www.nlverkiezingen.com/TK" + Jaargetal +".html").read()
soup = BeautifulSoup(r, "html.parser")
tables = soup.find_all("table")
for table in tables:
header = soup.find_all("h1")[0].getText()
print header
trs = table.find_all("tr")[0].getText()
print '\n'
for tr in table.find_all("tr"):
print "|".join([x.get_text().replace('\n','') for x in tr.find_all('td')])
Ive
tryedfrom bs4 import BeautifulSoup
import urllib
jaren = [str("2010"), str("2012")]
for Jaargetal in jaren:
r = urllib.urlopen("http://www.nlverkiezingen.com/TK" + Jaargetal +".html").read()
soup = BeautifulSoup(r, "html.parser")
tables = soup.find_all("table")
for table in tables:
header = soup.find_all("h1")[0].getText()
print header
for tr in table.find_all("tr"):
firstTd = tr.find("td")
if firstTd and firstTd.has_attr("class") and "l" in firstTd['class']:
tds = tr.find_all("td")
for tr in table.find_all("tr"):
print "|".join([x.get_text().replace('\n','') for x in tr.find_all('td')])
break
Was mache ich falsch oder was ich zu tun habe, bin ich auf dem richtigen Weg?
Könnten Sie zeigen, was falsch ist genau mit dem vorhandenen Code? Vielen Dank. – alecxe
@alecxe Im ersten Code druckt es alle Zeilen: Partij | Afk. | Aantal |% | +/- | Zetels Ich möchte den Code nur die Zeilen drucken lassen: Afk. Aantal und Zetels. – Danisk