属性错误和HTTP错误404试图从网站上抓取图像

问题描述 投票:0回答:1

我正在尝试从网站列表中获取所有图像。但是我得到他以下错误消息:

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'])

谢谢

python web-scraping beautifulsoup
1个回答
0
投票

您可以尝试

from bs4 import BeautifulSoup as bs4

images=[]

list_1=["file_1.csv"]

df = pd.read_csv("path")

for link in list_1:
    html = urlopen(link0])
    bs = bs4(html, 'html.parser')
    images = bs4.find_all('img')

    for image in images: 
            images.append(image['src'])

您收到的错误是您用{}查找的find_all方法的第一个错误,该错误为空,并且404错误归因于urlopen方法找不到您提供的链接。

© www.soinside.com 2019 - 2024. All rights reserved.