因此,本质上您想解析出
html文件以获取文件中的元素。您可以将BeautifulSoup或lxml用于此任务。
您已经有使用的解决方案
BeautifulSoup。我将使用发布解决方案
lxml:
from lxml import etreeimport urllibweb = 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:]]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)