如何在beautifulsoup中基于文本查找元素忽略子标签 [英] How to find element based on text ignore child tags in beautifulsoup

查看:463
本文介绍了如何在beautifulsoup中基于文本查找元素忽略子标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用Python和BeautifulSoup的解决方案,以基于内部文本查找元素.例如:

I am looking for a solution using Python and BeautifulSoup to find an element based on the inside text. For example:

<div> <b>Ignore this text</b>Find based on this text </div>

如何找到该div?谢谢您的帮助!

How can I find this div? Thanks for you helps!

推荐答案

您可以将.findtext参数一起使用,然后将findParent用作父元素.

You can use .find with the text argument and then use findParent to the parent element.

例如:

from bs4 import BeautifulSoup
s="""<div> <b>Ignore this text</b>Find based on this text </div>"""
soup = BeautifulSoup(s, 'html.parser')
t = soup.find(text="Find based on this text ") 
print(t.findParent())

输出:

<div> <b>Ignore this text</b>Find based on this text </div>

这篇关于如何在beautifulsoup中基于文本查找元素忽略子标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆