某些非美国股票代码的雅虎财务网络抓取现金流量表的问题

时间:2021-07-22 10:25:57

标签: python yahoo-finance yahoo-api yfinance

下面的代码是从雅虎财经中提取现金流量表。它适用于股票代码“AAPL”,但不适用于马来西亚交易所的“0200.KL”。有什么办法解决吗?

附加问题:是否有其他来源(例如 Alpha Vantage)可以捕获实时股票数据(例如股价和财务报表)? (ps:Alpha Vantage 似乎不适用于大马交易所股票行情)。尽管雅虎财经网站上显示了财务报表数据,但 yfinance 也无法提取马来西亚股票行情的财务报表。

import re 
import requests
from bs4 import BeautifulSoup
import json

#tickers="AAPL" #uncomment this and see how it works
tickers="0200.KL" #this ticker not works, it is a stock in Bursa Malaysia Exchange


url_financials=f"https://finance.yahoo.com/quote/{tickers}/financials?={tickers}"

user_agent = {"User-Agent": "Mozilla/5.0"} 
response = requests.get(url_financials,headers=user_agent)

soup = BeautifulSoup(response.text,"html.parser")
pattern = re.compile(r"\s--\sData\s--\s")
script_data = soup.find("script",text=pattern).contents[0]

start=script_data.find("context") - 2   #find method returns the POSITION of the argument or the object
json_data=json.loads(script_data[start:-12])

annual_cashflow = json_data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]["cashflowStatementHistory"]["cashflowStatements"]

smts=[]

for s in annual_cashflow:
    statements={}
    for key, val in s.items():
        try:
            statements[key]=val["raw"]
        except KeyError:
            continue
        except TypeError:
            continue
    smts.append(statements)
smts

0 个答案:

没有答案