【发布时间】:2015-05-19 07:48:35
【问题描述】:
我正在尝试使用 Python Beautiful Soup 4 库解析一个大型 html 文档。
该页面包含一个非常大的表格,结构如下:
<table summary='foo'>
<tbody>
<tr>
A bunch of data
</tr>
<tr>
More data
</tr>
.
.
.
100s of <tr> tags later
</tbody>
</table>
我有一个函数可以评估soup.descendants 中的给定标签是否属于我正在寻找的那种。这是必要的,因为页面很大(BeautifulSoup 告诉我文档包含大约 4000 个标签)。
是这样的:
def isrow(tag):
if tag.name == u'tr':
if tag.parent.parent.name == u'table' and \
tag.parent.parent.has_attr('summary'):
return True
我的问题是,当我遍历 soup.descendants 时,该函数仅返回表中前 77 行的 True,而我知道 <tr> 标记继续存在数百行。
这是我的函数的问题,还是我不了解 BeautifulSoup 如何生成其后代集合?我怀疑这可能是 Python 或 bs4 内存问题,但我不知道如何进行故障排除。
【问题讨论】:
标签: python html web-scraping beautifulsoup