使用BeautifulSoup有几种方法来查找多个div/div/div/div/img标签
最好详细些,,举例最好 您可以使用BeautifulSoup来查找多个div/div/div/div/img标签的方法有多种。以下是两种常用的方法:方法一:使用select方法
from bs4 import BeautifulSoup
html = """
<div>
<div>
<div>
<div>
<img src="image1.jpg">
</div>
</div>
</div>
<div>
<div>
<div>
<img src="image2.jpg">
</div>
</div>
</div>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
images = soup.select('div div div div img')
for img in images:
print(img['src'])
方法二:使用find_all方法
from bs4 import BeautifulSoup
html = """
<div>
<div>
<div>
<div>
<img src="image1.jpg">
</div>
</div>
</div>
<div>
<div>
<div>
<img src="image2.jpg">
</div>
</div>
</div>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
divs = soup.find_all('div')
for div in divs:
imgs = div.find_all('img')
for img in imgs:
print(img['src'])
以上代码分别演示了使用select方法和find_all方法来查找多个div/div/div/div/img标签的示例。
页:
[1]