Python 我需要选择一个div标记,该标记具有特定的子标记beautifulsoup X1
我需要选择具有特定h4匹配文本X1的div。通过使用具有特定属性的find_all(),然后再次使用find_all转到h4,可以选择h4而不是div本身。我需要选择divPython 我需要选择一个div标记,该标记具有特定的子标记beautifulsoup X1,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我需要选择具有特定h4匹配文本X1的div。通过使用具有特定属性的find_all(),然后再次使用find_all转到h4,可以选择h4而不是div本身。我需要选择div 如果上述方法可行,是否有任何直接访问“a”标记的方法?看起来您需要findNext方法 Ex: <div class = "some class"> <h4>X1</h4> <a href="www.someurl.com">Value of X1</a&
如果上述方法可行,是否有任何直接访问“a”标记的方法?看起来您需要
findNext
方法
Ex:
<div class = "some class">
<h4>X1</h4>
<a href="www.someurl.com">Value of X1</a>
</div>
from bs4 import BeautifulSoup
html = """<div class = "some class">
<h4>X1</h4>
<a href="www.someurl.com">Value of X1</a>
</div>"""
soup = BeautifulSoup(html, "html.parser")
for tag in soup.find_all("h4", text="X1"): #Find all h4 with required text.
print(tag.findNext("a").text)
Value of X1