,我试图从Tripadvisor得到一些评价数据,但我试图获取我渐渐NoneType在python
数据“NoneType”对象未标化的
任何人可以帮助我想知道我错在哪里,对不起,我对python很陌生。
这里是我的示例代码
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'])
输出到这个样子:
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
'rate.img is None' ... – jonrsharpe
这意味着在您的至少一个'rate' div下没有''标签。 –