简体   繁体   English

AttributeError:'NoneType'对象没有属性'text'-python

[英]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.

相关问题 AttributeError:“ NoneType”对象在python中没有属性“ text” - AttributeError: 'NoneType' object has no attribute 'text' in python (Python) AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - (Python) AttributeError: 'NoneType' object has no attribute 'text' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39; - Python: AttributeError: 'NoneType' object has no attribute 'text' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;-Python,BeautifulSoup错误 - AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;beautifulsoup python - AttributeError: 'NoneType' object has no attribute 'text' beautifulsoup python Python-AttributeError:“ NoneType”对象没有属性“ get_text” - Python - AttributeError: 'NoneType' object has no attribute 'get_text' Python XML 元素属性错误:'NoneType' object 没有属性'文本' - Python XML Element AttributeError: 'NoneType' object has no attribute 'text' Python XML AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39; - Python XML AttributeError: 'NoneType' object has no attribute 'text' Python-Kivy AttributeError: 'NoneType' object 没有属性 'text' - Python-Kivy AttributeError: 'NoneType' object has no attribute 'text' 使用 Python3 / AttributeError 抓取网站:'NoneType' object 没有属性 'text' - Scraping website with Python3 / AttributeError: 'NoneType' object has no attribute 'text'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM