因此,本质上您想解析出html
文件以获取文件中的元素。您可以将BeautifulSoup或lxml用于此任务。
您已经有使用的解决方案BeautifulSoup
。我将使用发布解决方案lxml
:
from lxml import etree
import urllib
web = urllib.request.urlopen("http://www.ffiec.gov/census/report.aspx?year=2011&state=01&report=demographic&msa=11500")
s = web.read()
html = etree.HTML(s)
## Get all 'tr'
tr_nodes = html.xpath('//table[@id="Report1_dgReportDemographic"]/tr')
## 'th' is inside first 'tr'
header = [i[0].text for i in tr_nodes[0].xpath("th")]
## Get text from rest all 'tr'
td_content = [[td.text for td in tr.xpath('td')] for tr in tr_nodes[1:]]