简体   繁体   中英

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. 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'] . You can just use article[src] to get the relative image URL in your example. Combine this with the main URL, and you should be able to get the images. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM