r = requests.get(link)
soup = BeautifulSoup(r.content, 'lxml')
images = [x ['src'] for x in soup.find("div",{"class":"rev-slider"}).find_all("img")]
This code run perfectly when the div has more than one image but in some pages there is only one image in this div so it stop working and showing this error:
AttributeError: 'NoneType' object has no attribute 'find_all'
I tried to add it in if else but not find any solution.
CodePudding user response:
At some point, your soup.find("div",{"class":"rev-slider"})
is returning None. You need logic to handle it when that <div>
and class is not present.
r = requests.get(link)
soup = BeautifulSoup(r.content, 'lxml')
images = []
for x in soup.find("div",{"class":"rev-slider"}):
try:
images = [i['src'] for i in x.find_all("img")]
except Exception as e:
print(e)