我试图访问一个巨大的html文件中的表。该表在目录中引用。因此,我编写了获取href的代码。然后我使用href值来定位表。它有时起作用。否则,它将返回none。以下是html练习: 目录:
<a href="#h54"><font size="2">Summary Compensation Table</font></a>
表位置位于此标记下方:
<a name="h54"></a>
这是我的密码:
def your_filter(tag, value):
return any(tag[key] == value for key in tag.attrs.keys())
all_linked = soup.find_all("a", text=re.compile(r'summary compensation', re.IGNORECASE), href=True)
if len(all_linked)>0:
table_link = all_linked[0]['href']
tags = soup.find_all(lambda tag: your_filter(tag, table_link[1:]))
goto_table = soup.find(tags[0].name, tags[0].attrs)
这里标签不返回任何值
DOM是相当“扁平”的,当您实际上需要在DOM的更高层次上,在父div的级别上,查找嵌套元素,然后查找具有目标表的同级
div
。一种方法可能是:相关问题 更多 >
编程相关推荐