繁体   English   中英

使用 Python3 / AttributeError 抓取网站:'NoneType' object 没有属性 'text'

[英]Scraping website with Python3 / AttributeError: 'NoneType' object has no attribute 'text'

我正在尝试抓取一个网站,但我不断收到AttributeError: 'NoneType' object has no attribute 'text'错误。 任何帮助将不胜感激。

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = 'https://www.bloomberg.com/quote/SPX:IND'    
page = urlopen(html)    
data = BeautifulSoup(page, 'html.parser')    
name_box = data.find('h1', attrs={'class': 'companyName__99a4824b'}) 
name = name_box.text.strip() 
print(name)

你的 name_box 是 None 因此问题在于找到 h1 行:

name_box = data.find('h1', attrs={'class': 'companyName__99a4824b'})

也许 class 不正确

如果您查看请求的 output(通过print(data) ),您可以看到您的请求被他们的机器人过滤器捕获。

见: 这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM