NoneType in python
Question: I was trying to get some rating data from
[Tripadvisor](http://'http://www.tripadvisor.in/Hotels-g186338-London_England-
Hotels.html') but as i was trying to fetch the data i was getting

> 'NoneType' object is not subscriptable

Can anybody help me figuring out where am i going wrong , sorry i am very new
to python.

Here is my sample code

    
    
    import requests
    import re
    from bs4 import BeautifulSoup
    r = requests.get('http://www.tripadvisor.in/Hotels-g186338-London_England-Hotels.html')
    data = r.text        
    soup = BeautifulSoup(data)
    for rate in soup.find_all('div',{"class":"rating"}):
                   print (rate.img['alt'])
    

The output to this looks like:

    
    
    4.5 of 5 stars
    4.5 of 5 stars 4 of 5 stars
    4.5 of 5 stars
    4.5 of 5 stars 4 of 5 stars
    4.5 of 5 stars
    4.5 of 5 stars
    4.5 of 5 stars Traceback (most recent call last):
    
      File "<ipython-input-52-7460e8bfcb82>", line 3, in <module>
        print (rate.img['alt'])
    
    TypeError: 'NoneType' object is not subscriptable
    


Answer: 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](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-
selectors):

    
    
    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.

