python - 使用 BeautifulSoup 的 python 股票价格
问题描述
我正在尝试使用以下代码获取股票的价格,它为当前价格返回 null。请让我知道我在哪里出错
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
my_url = 'https://finance.yahoo.com/quote/MMM/key-statistics?p=MMM'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup =soup(page_html,"lxml")
uClient.close()
# I tried this option 1
currentPrice = page_soup.find('div',attrs={"span": "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"})
print(currentPrice)
# I tried this option 2
for currentPrice in page_soup.find("div",{"class": "D(ib) Mend(20px)"}) :
print (page_soup.span)
解决方案
您可能想看看 yfinance https://pypi.org/project/yfinance/
推荐阅读
- javascript - Jest vue3:SyntaxError:意外的令牌“导出”(信号)
- windows - 如何获取不包含特定类型文件的文件夹/子文件夹列表?
- gradle - 在 Android Studio 中允许 HTTP 下载
- google-api - googleapi servicenetworking上的terraform后端500错误
- c# - 如何使用反射获取没有获取/设置的属性?
- django - 在 pycharm 中运行 django 时出现错误 (root) Additional property django is not allowed
- c++ - 巴比伦平方根算法输出不匹配示例
- visual-studio-code - 如何将 Visual Studio 代码输出路径颜色从白色更改为绿色。我被卡住了,有人请帮我设置一下吗?
- python - 有没有办法在按下按钮时先打开红色 LED,然后打开蓝色 LED,最后打开绿色 LED?
- java - 如何使用 Spring 将运行时创建的字符串注入另一个类