简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'find_all'

I am trying to scrape the link of image from: url='https://www.hydac.com/shop/en/1250064#simple-downloads'

Here is the error: AttributeError: 'NoneType' object has no attribute 'find_all'

I dont know what is going wrong. Here is my code:

r=requests.get(link)
soup = BeautifulSoup(r.text,'html.parser')

images=soup.find('div',attrs={'class':'fotorama__stage__frame fotorama_vertical_ratio fotorama__loaded fotorama__loaded--img fotorama__active'})

all_images=images.find_all('img')
for image in all_images:
    print(image['src'])

You are assuming that
images=soup.find('div',attrs={'class':'fotorama__stage__frame fotorama_vertical_ratio fotorama__loaded fotorama__loaded--img fotorama__active'})
will find something.

What is happening here is that it is finding nothing, since it tells you that images is a NoneType (therefore has no find_all attribute)

So you should add an if statement to handle this edge case where it does not find anything.

To get image link of main product photo you can use next example:

import requests
from bs4 import BeautifulSoup

url = "https://www.hydac.com/shop/en/1250064#simple-downloads"

soup = BeautifulSoup(requests.get(url).content, "html.parser")
print(soup.select_one('[alt="main product photo"]')["data-src"])

Prints:

https://www.hydac.com/shop/media/catalog/crossbase/cache/804510ce68ff34894246cc79cc94a424/PRD_PHO_3D/PRD_PHO_3D_10000020495-00002__SALL__AIN__V1.jpg

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