[英]AttributeError: 'NoneType' object has no attribute 'text' - python
So I've been trying to learn data scraping and I'm using this stock website 所以我一直在尝试学习数据抓取,并且我正在使用这个库存网站
when I inspect element the price, it shows that: 当我检查元素的价格时,它表明:
<span class="priceText__1853e8a5">12,620.83</span>
So when I use that and write this in python: 因此,当我使用它并用python编写时:
stockPrice = soup.find('div', class_="priceText__1853e8a5")
price = stockPrice.text.strip()
print(price)
It gives me the following error when I run it: 运行它时,出现以下错误:
price = stockPrice.text.strip()
AttributeError: 'NoneType' object has no attribute 'text'
However, when I use: 但是,当我使用:
stockPrice = soup.find('div', class_="price")
The program runs completely fine. 该程序运行完全正常。 Why is that?
这是为什么? there is no div with class = "price".
没有class =“ price”的div。 I'm really confused.
我真的很困惑
I've never scraped before, but there's a .get_text()
method . 我从来没有刮过,但是有一个
.get_text()
方法 。
Use as follows: 用法如下:
from bs4 import BeautifulSoup
import requests
r = requests.get("https://www.bloomberg.com/quote/NYA:IND")
data = r.text
soup = BeautifulSoup(data, "lxml")
stockPrice = soup.find("div", class_="price")
print(stockPrice)
price = stockPrice.get_text()
print(price)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.