我想在div
标签下抓取多张图片,但出现错误
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
html示例
<div class="class_name">
<img src="#">
<img src="#">
<img src="#">
<img src="#">
</div>
python 3
import requests
import urllib.request
import time
from bs4 import BeautifulSoup
url = 'web_url'
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
div = soup.findAll('div', {"class": "class_name"})
img = div.find_all('img')
for image in img:
print(image['src'])
答案 0 :(得分:1)
尝试使用此代码提取所有图像的src=...
:
from bs4 import BeautifulSoup
txt = '''<div class="class_name">
<img src="#1">
<img src="#2">
<img src="#3">
<img src="#4">
</div>'''
soup = BeautifulSoup(txt, 'html.parser')
for d in soup.select('div.class_name'):
for i in d.select('img'):
print(i['src'])
打印:
#1
#2
#3
#4