例如
bs = BeautifulSoup("<html><a>sometext</a></html>")
print bs.find_all("a",text=re.compile(r"some"))
返回[<a>sometext</a>]
,但当搜索的元素有子元素时,即img
bs = BeautifulSoup("<html><a>sometext<img /></a></html>")
print bs.find_all("a",text=re.compile(r"some"))
它返回[]
有没有办法用find_all
来匹配后面的例子?
您将需要使用混合方法,因为当元素既有文本又有子元素时,
text=
将失败。背景
当BeautifulSoup正在搜索元素,并且
text
是可调用的时,它最终eventually calls:在您给出的两个示例中,
.string
方法返回不同的内容:.string
方法如下所示:如果我们打印出内容,我们可以看到为什么返回
None
:相关问题 更多 >
编程相关推荐