Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要选择一个div标记,该标记具有特定的子标记beautifulsoup X1_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 我需要选择一个div标记,该标记具有特定的子标记beautifulsoup X1

Python 我需要选择一个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&

我需要选择具有特定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>
</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