P
P
ParnishkaSPB2020-06-08 13:52:33
Python
ParnishkaSPB, 2020-06-08 13:52:33

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

1 answer(s)
S
Sergey Karbivnichy, 2020-06-08
@ParnishkaSPB

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

The page has an empty li tag with the item class. Most likely, an ad block is embedded in this empty tag, but since adblock blocks ads, the block turns out to be empty, so an exception is obtained.
5ede2176ede6a830455430.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question