简体   繁体   English

AttributeError: 'NoneType' object 没有属性 'attrs',我在尝试从 url 抓取图像时遇到此错误。 我该如何解决?

[英]AttributeError: 'NoneType' object has no attribute 'attrs', I am getting this error while I am trying to scrape images from the url. How can I fix it?

import bs4
import requests
url ="https://www.passiton.com/inspirational-quotes?page=2"
response = requests.get(url)
soup = bs4.BeautifulSoup(response.content)
article_element = soup.findAll('img')

for i, article in enumerate(article_element):
with open('inspiration{}.jpg'.format(i),'wb') as file:
    img_url = article.img.attrs['src']
    response = requests.get(img_url)

    file.write(response.content)

AttributeError: 'NoneType' object has no attribute 'attrs', I am getting this error while trying to scrapping images from the url. AttributeError: 'NoneType' object 没有属性 'attrs',我在尝试从 url 抓取图像时遇到此错误。 How can I fix it?我该如何解决? Please help me with this error请帮我解决这个错误

I'm not sure what you want to achieve with article.img.attrs['src'] .我不确定你想用article.img.attrs['src']实现什么。 You can just use article[src] to get the relative image URL in your example.在您的示例中,您可以只使用article[src]来获取相对图像 URL。 Combine this with the main URL, and you should be able to get the images.将其与主要的 URL 结合,您应该能够获得图像。 This should fix your error:这应该可以解决您的错误:


import bs4
import requests
main_url = "https://www.passiton.com/"
url ="https://www.passiton.com/inspirational-quotes?page=2"
response = requests.get(url)
soup = bs4.BeautifulSoup(response.content)
article_element = soup.findAll('img')

for i, article in enumerate(article_element):
with open('inspiration.jpg','wb') as file:
    img_url = main_url+article['src']
    response = requests.get(img_url)
    file.write(response.content)

to avoid crash if no key in dict, use为避免在字典中没有键时崩溃,请使用

dict.get('key')  # returns value or None

instead of代替

dict['key']  # returns value or crash

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在执行 PCA 时遇到此错误:- AttributeError: 'NoneType' object has no attribute 'split' - Why i am getting this error while doing PCA :- AttributeError: 'NoneType' object has no attribute 'split' 为什么我会收到“AttributeError: 'NoneType' object has no attribute 'get'” - why am i getting "AttributeError: 'NoneType' object has no attribute 'get' " 我正在尝试将具有一些空属性的xml文件导入表。 收到此错误AttributeError:'NoneType'对象没有属性'strip' - I am trying to import xml file with some empty attributes to table. getting this error AttributeError: 'NoneType' object has no attribute 'strip' 我在“AttributeError:'NoneType'对象中没有属性'get_all_permissions'中收到这些错误 - I am getting these error in " AttributeError: 'NoneType' object has no attribute 'get_all_permissions' 为什么我在 /admin/main/consultation/'NoneType' object 没有属性 'lastname' 处出现 AttributeError 错误 - why am i getting error of AttributeError at /admin/main/consultation/ 'NoneType' object has no attribute 'lastname' 为什么我收到“AttributeError: 'NoneType' object has no attribute 'send'”错误 - Why am I getting a `AttributeError: 'NoneType' object has no attribute 'send'` Error 我收到类似 AttributeError: 'NoneType' object has no attribute 'text' 的错误 - I am getting error like AttributeError: 'NoneType' object has no attribute 'text' 为什么我会收到 AttributeError: 'NoneType' object has no attribute 'send' 以及如何避免它? - Why am I getting AttributeError: 'NoneType' object has no attribute 'send' and how to avoid it? 我该如何解决这个问题:AttributeError: 'NoneType' object has no attribute 'strip - How can i fix this :AttributeError: 'NoneType' object has no attribute 'strip 我该如何修复,AttributeError: 'NoneType' 对象没有属性 'send' - How can I fix, AttributeError: 'NoneType' object has no attribute 'send'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM