我正在尝试从网站列表中获取所有图像。但是我得到他以下错误消息:
1)AttributeError:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-52-d786eb50d187> in <module>
14 html = urlopen(link)
15 bs = bs4(html, 'html.parser')
---> 16 images = bs4.find_all('img', {})
17
18 for image in images:
/anaconda3/lib/python3.7/site-packages/bs4/element.py in find_all(self, name, attrs, recursive, text, limit, **kwargs)
1698 :rtype: bs4.element.ResultSet
1699 """
-> 1700 generator = self.descendants
1701 if not recursive:
1702 generator = self.children
AttributeError: 'str' object has no attribute 'descendants'
和
2)HTTP 404
HTTPError: HTTP Error 404: Not Found
您知道它们是什么,我该如何解决?
我正在使用的代码如下:
from bs4 import BeautifulSoup as bs4
images=[]
list_1=["file_1.csv"]
df = pd.read_csv("path"+list_1)
for link in df["Col"]:
html = urlopen(link)
bs = bs4(html, 'html.parser')
images = bs4.find_all('img', {})
for image in images:
images.append(image['src'])
谢谢
答案 0 :(得分:0)
您可以尝试
from bs4 import BeautifulSoup as bs4
images=[]
list_1=["file_1.csv"]
df = pd.read_csv("path"+list_1)
for link in df["Col"]:
try:
html = urlopen(link)
except Exception:
continue
bs = bs4(html, 'html.parser')
images = bs4.find_all('img')
for image in images:
images.append(image['src'])
您收到的错误是您用{}查找的find_all方法的第一个错误,该错误为空,而404错误是由于urlopen方法找不到您提供的链接。