python - AttributeError:“NoneType”对象没有属性“文本”-python
问题描述
所以我一直在尝试学习数据抓取,我正在使用 这个股票网站
当我检查元素价格时,它表明:
<span class="priceText__1853e8a5">12,620.83</span>
因此,当我使用它并在 python 中编写它时:
stockPrice = soup.find('div', class_="priceText__1853e8a5")
price = stockPrice.text.strip()
print(price)
当我运行它时,它给了我以下错误:
price = stockPrice.text.strip()
AttributeError: 'NoneType' object has no attribute 'text'
但是,当我使用:
stockPrice = soup.find('div', class_="price")
该程序运行完全正常。这是为什么?没有 class = "price" 的 div。我真的很困惑。
解决方案
我以前从来没有刮过,但是有一种.get_text()
方法。
使用如下:
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)
推荐阅读
- python - 如何使用 python(最好是 BS4)从 Google 图片(或 bing)中找到图片的 url?
- javascript - 如何在反应中从firebase cloud firestore获取数据
- amazon-web-services - 注册 ECS Fargate 实例时 NLB 健康检查随机失败
- sql - 创建滚动期间
- python - 如何将连接的值连接到python中的新值
- f# - 在 F# 中,如何将 null 写入 Nullable
班员? - java - 单击事件时跳转到集合中的下一个项目
- azure-devops - .NET Core SDK 5.0.100 的 Nuget 还原失败
- pine-script - Change chart background daily
- wordpress - 网站更新后自动刷新页面