이 사이트의 정보 (HTML 테이블) 구문 분석하려고 : 내가 BeautifulSoup로 사용하고 현재 http://www.511virginia.org/RoadConditions.aspx?j=All&r=1BeautifulSoup로 HTML 테이블 분석
내가 문제가 함께이
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
같은 외모를 가지고있는 코드를 시작과 끝의 열. 그들은 단지 "없음"으로 인쇄되는
출력 :
(u'Rt. 613N (Giles County)', None, None, u'Moderate', u'snow or ice', u'01/13/2010 10:50 AM')
나는 그들이 열 목록에 저장받을 것을 알고 있지만, 별도의 링크 태그는 원래의 HTML보고와 분석을 망치는 것 같다
<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>
그래서 무엇을 인쇄해야하는 것은 :
(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')
모든 suggesti이 같은 도움이나 도움을 주시고 미리 감사드립니다.
감사합니다. 많이 –
당신은 아름다운 스프를 사용할 필요가 없습니다. 당신은 python3 htmlparser를 사용할 수있다 : https://github.com/schmijos/html-table-parser-python3/blob/master/html_table_parser/parser.py – schmijos