下面的代码是从yahoo finance中提取现金流量表。它适用于马来西亚布尔萨的股票代码“AAPL”,但不适用于“0200.KL”。有办法解决吗
附加问题:是否有其他来源,如Alpha Vantage,用于捕获实时股票数据,如股票价格和财务报表?(注: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
目前没有回答
相关问题 更多 >
编程相关推荐