问题描述
当我尝试在 yahoo_fin(见下文)的帮助下从苹果下载当前价格时,我收到错误消息 KeyError: 'adjclose'。 任何人都知道为什么以及如何解决它?
谢谢
from yahoo_fin import stock_info as si
ab=si.get_live_price('APPL')
print(ab) ´´´
Complete Erro massage:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-71-71025ce2a3a4> in <module>
1 #self._history["Current Price"]=self._history["Ticker"].apply(lambda x: si.get_live_price(x))
----> 2 ab=si.get_live_price('APPL')
3 ab
~/opt/anaconda3/envs/Financial-Project/lib/python3.7/site-packages/yahoo_fin/stock_info.py in get_live_price(ticker)
397 '''
398
--> 399 df = get_data(ticker,end_date = pd.Timestamp.today() + pd.DateOffset(10))
400
401
~/opt/anaconda3/envs/Financial-Project/lib/python3.7/site-packages/yahoo_fin/stock_info.py in get_data(ticker,start_date,end_date,index_as_date,interval)
86
87 # add in adjclose
---> 88 frame["adjclose"] = data["chart"]["result"][0]["indicators"]["adjclose"][0]["adjclose"]
89
90 # get the date info
KeyError: 'adjclose'
´´´
解决方法
手册上说它不区分大小写,但您可以使用小写字母。
from yahoo_fin import stock_info
ab=stock_info.get_live_price('aapl')
print(ab)
129.41000366210938
,
Apple 股票代码是“AAPL”(不是“APPL”)。您的代码可以通过该更新正确运行。
from yahoo_fin import stock_info as si
ab=si.get_live_price('AAPL')
print(ab)
134.99000549316406