Answer the question
In order to leave comments, you need to log in
It gives an error when parsing the page, how to fix it?
There is a code:
response = requests.get(' https://101hotels.com/recreation/russia/sankt-pete... ').text
html = BS(responce, 'html.parser')
items = html.find_all( 'li', class_='item')
d = []
for item in items:
d.append({
'title': item.find('div', class_='item-name').text,
'address' : item.find('span', class_='item-address'),
'p': item.find('div', class_='item-description')
})
print (d)
When I press execute it writes an error : 'title': item.find('div', class_='item-name').text, AttributeError: 'NoneType' object has no attribute 'text', although there is text in the class, but for some reason he does not see it, how to fix it?
Answer the question
In order to leave comments, you need to log in
You need to either do a check or wrap the code in a try...except:
for item in items:
try:
d.append({
'title': item.find('div', class_='item-name').text,
'address': item.find('span', class_='item-address').text,
'p': item.find('div', class_='item-description').text.replace('\xa0','')
})
except:
pass
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question