![](/img/trans.png)
[英]why am i getting "AttributeError: 'NoneType' object has no attribute 'get' "
[英]Web Scraper: Why am I getting AttributeError: 'NoneType' object has no attribute 'text'?
我正在制作我的第一個Web爬蟲,該爬蟲應該從Bloomberg.com返回S&P 500索引,但是當我嘗試運行它時,卻收到以下錯誤消息:AttributeError:'NoneType'對象沒有屬性'text'。
我用作參考的代碼(來自https://www.freecodecamp.org/news/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe/ )使用urllib2,據我了解,該代碼已拆分為多個庫。 所以我不確定這是問題嗎?
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = 'https://www.bloomberg.com/quote/SPX:IND'
htmlpage = urlopen(url)
soup = BeautifulSoup(htmlpage, 'html.parser')
name_box = soup.find('h1', attrs={'class':'name'})
name = name_box.text.strip(0)
print(name)
price_box = soup.find('div', attrs={'class':'price'})
price = price_box.text
print(price)
它應該從Bloomberg.com返回標准普爾500指數。
可能找不到您要查找的元素。 您可以通過評論來檢查
name = name_box.text.strip(0)
price = price_box.text
並驗證name
和price
的值
另外,請確保檢查soup
的HTML,以確保請求成功並且所需的元素可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.