I was trying to get some rating data from Tripadvisor but as i was trying to fetch the data i was getting
\'NoneType\' object is not subscriptable
Not all your <div class="rating"> tags have an <img /> tag, so rate.img is None.
Those divs look like this instead:
<div class="rating">
<span class="rate">4.5 out of 5, </span>
<em>2,294 Reviews</em>
<br/>
<div class="posted">Last reviewed 25 Sep 2015</div>
</div>
You can either test for this:
if rate.img is not None:
# ...
or select only images under the div.rating tags with a CSS selector:
for img in soup.select('div.rating img[alt]'):
The selector here picks out <img/> tags with an alt attribute, nested inside a <div class="rating"> tag.
It means that not all divs with a class of rating have images with an alt attribute. You should handle this appropriately - to ignore such cases, just wrap your print (rate.img['alt']) in a try, except block, or check to see if rate.img is None first.
First option:
try:
print(rate.img['alt'])
except TypeError:
print('Rating error')
Second option:
for rate in soup.find_all('div',{"class":"rating"}):
if rate.img is not None:
print (rate.img['alt'])
The first option follows EAFP (Easier to ask for forgiveness than permission), a common Python coding style, whereas the second follows LBYL (Look before you leap). In this case, I would suggest the second.