,我試圖從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下沒有''標籤。 –