BeautifulSoup4 i tag within a tag breaks search by tex(标签内的BeautifulSoup4 i标签按tex分隔搜索)

2024-05-28 19:30:46 发布

您现在位置:Python中文网/ 问答频道 /正文

期望值:

>>> s = BeautifulSoup('<a href="http://example.com">lele</a>')
>>> s.find('a', text='lele')
<a href="http://lele.com">lele</a>

意外:

>>> s = BeautifulSoup('<a href="http://example.com">lele<i><i></a>')
>>> s.find('a', text='lele')              # return None
>>> s.find('a', text=re.compile('lele'))  # return None

因此,在意外的情况下,a标记中的i标记似乎会通过文本破坏搜索功能,即使使用regex也是如此。有没有一种方法可以让文本搜索在意外情况下工作


Tags: text标记文本recomnonehttpreturn

热门问题