问题
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
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/32799480/nonetype-in-python