您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Python从网站中提取表格

如何使用Python从网站中提取表格

因此,本质上您想解析出html文件获取文件中的元素。您可以将BeautifulSouplxml用于此任务。

您已经有使用的解决方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:]]
python 2022/1/1 18:37:40 有525人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶