Ich versuche, Informationen (HTML-Tabellen) von dieser Seite zu analysieren: http://www.511virginia.org/RoadConditions.aspx?j=All&r=1BeautifulSoup HTML-Tabelle Parsing
Derzeit ich BeautifulSoup verwende und der Code, den ich wie folgt aussieht haben mit
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
mech = Browser()
url = "http://www.511virginia.org/RoadConditions.aspx?j=All&r=1"
page = mech.open(url)
html = page.read()
soup = BeautifulSoup(html)
table = soup.find("table")
rows = table.findAll('tr')[3]
cols = rows.findAll('td')
roadtype = cols[0].string
start = cols.[1].string
end = cols[2].string
condition = cols[3].string
reason = cols[4].string
update = cols[5].string
entry = (roadtype, start, end, condition, reason, update)
print entry
Das Problem ist, die Anfangs- und Endspalten. Sie bekommen nur als „None“ gedruckt
Ausgang:
(u'Rt. 613N (Giles County)', None, None, u'Moderate', u'snow or ice', u'01/13/2010 10:50 AM')
Ich weiß, dass sie in der Liste Spalten gespeichert zu bekommen, aber es scheint, dass der zusätzliche Link-Tag wird das Parsen mit der ursprünglichen HTML vermasselt suchen wie folgt aus:
<td headers="road-type" class="ConditionsCellText">Rt. 613N (Giles County)</td>
<td headers="start" class="ConditionsCellText"><a href="conditions.aspx?lat=37.43036753&long=-80.51118005#viewmap">Big Stony Ck Rd; Rt. 635E/W (Giles County)</a></td>
<td headers="end" class="ConditionsCellText"><a href="conditions.aspx?lat=37.43036753&long=-80.51118005#viewmap">Cabin Ln; Rocky Mount Rd; Rt. 721E/W (Giles County)</a></td>
<td headers="condition" class="ConditionsCellText">Moderate</td>
<td headers="reason" class="ConditionsCellText">snow or ice</td>
<td headers="update" class="ConditionsCellText">01/13/2010 10:50 AM</td>
so was gedruckt ist werden soll:
(u'Rt. 613N (Giles County)', u'Big Stony Ck Rd; Rt. 635E/W (Giles County)', u'Cabin Ln; Rocky Mount Rd; Rt. 721E/W (Giles County)', u'Moderate', u'snow or ice', u'01/13/2010 10:50 AM')
Alle suggesti ons oder hilfe wird geschätzt, und danke im voraus.
Vielen Dank –
Sie müssen keine schöne Suppe dafür verwenden. Sie könnten python3 htmlparser verwenden: https://github.com/schmijos/html-table-parser-python3/blob/master/html_table_parser/parser.py – schmijos