如何从股票行情列表中从Yahoo Finance中刮取/提取特定的资产负债表字段?

2024-10-03 23:19:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个名为stocktickers.csv的文件中有一个tickers列表(如下所示):

^{tb1}$

我想从该列表中提取雅虎金融资产负债表数据,并将其输入stocktickers.csv文件,如下所示。“有形账面价值”和“共享已发行”是在每个股票发行人的资产负债表网页上找到的字段,如AMD的:https://finance.yahoo.com/quote/AMD/balance-sheet?p=AMD

^{tb2}$

这就是我到目前为止所拥有的,多年来它一直在刮平有形的账面价值

from bs4 import BeautifulSoup
import requests


header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
}

url = 'https://finance.yahoo.com/quote/AAPL/balance-sheet?p=AAPL'

response = requests.get(url, headers=header)

html = response.text
soup = BeautifulSoup(html, "html.parser")
main = soup.find("div", {"data-reactid": "195"}) #or 196
divs = main.find_all("div")

for div in divs:
    span = div.find("span")
    try:
        print(span.text)
    except:
        pass

结果:

   Tangible Book Value
Tangible Book Value
65,339,000
90,488,000
107,147,000
126,032,000

如果有一种方法可以使用get_balance_sheet()(从yfinance module)来刮取特定的资产负债表字段,比如上面的字段,那也太好了


Tags: 文件csvdiv列表htmlfindsheetspan