[英]BeautifulSoup4 find all tags with attribute begins with a string in Python
[英]Find all tags containing a string in BeautifulSoup
在 BeautifulSoup 中,我可以使用find_all(string='example')
查找與字符串或正則表達式匹配的所有 NavigableString。
有沒有辦法使用get_text()
而不是string
來執行此操作,以便搜索匹配一個字符串,即使它跨越多個節點? 即我想做類似的事情: find_all(get_text()='Python BeautifulSoup')
,這將匹配整個內部字符串內容。
例如,拿這個片段:
<body>
<div>
Python
<br>
BeautifulSoup
</div>
</body>
如果我想找到“Python Beautiful Soup”並讓它同時返回body
和div
標簽,我該怎么做呢?
您可以將css selectors
與偽 class 結合使用:-soup-contains-own()
soup.select_one(':-soup-contains-own("BeautifulSoup")')
或者只獲取元素的文本:
soup.select_one(':-soup-contains-own("BeautifulSoup")').get_text(' ', strip=True)
from bs4 import BeautifulSoup
html = '''
<body>
<div>
Python
<br>
BeautifulSoup
</div>
</body>
'''
soup = BeautifulSoup(html)
soup.select(':-soup-contains-own("BeautifulSoup")')
[<div>
Python
<br/>
BeautifulSoup
</div>]
您可以在 .find_all 中使用 lambda .find_all
:
from bs4 import BeautifulSoup
html_doc = '''\
<body>
<div>
Python
<br>
BeautifulSoup
</div>
</body>'''
soup = BeautifulSoup(html_doc, 'html.parser')
for tag in soup.find_all(lambda tag: 'Python BeautifulSoup' in tag.get_text(strip=True, separator=' ')):
print(tag.name)
印刷:
body
div
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.