首页 > 解决方案 > 使用 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)                                                    

标签: pythonyahoo-finance

解决方案


您可能想看看 yfinance https://pypi.org/project/yfinance/


推荐阅读