问题描述
我在下面编写了代码,但给出了错误。它给出了KeyError:'regularMarketopen'。
因此,我尝试从here查找解决方案并更新了base.py,但问题仍然存在。如何解决?
stk_list = ['A','AAL','AAP','AAPL','ABBV','ABC']
rows = []
for ticker in stk_list:
stk_container = yf.Ticker(ticker)
try:
stk_info = stk_container.info
rows.append(stk_info)
except IndexError as e:
print(f'{ticker}: {e}') #print the ticker and the error
df = pd.DataFrame(rows)
subset_df = df[['symbol','longName','sector','industry','trailingPE','pricetoBook','trailingEps','dividendYield']]
subset_df.head(10)
KeyError Traceback (most recent call last)
<ipython-input-8-15d77c81339f> in <module>
6 stk_container = yf.Ticker(ticker)
7 try:
----> 8 stk_info = stk_container.info
9 rows.append(stk_info)
10 except IndexError as e:
/opt/anaconda3/lib/python3.7/site-packages/yfinance/ticker.py in info(self)
136 @property
137 def info(self):
--> 138 return self.get_info()
139
140 @property
/opt/anaconda3/lib/python3.7/site-packages/yfinance/base.py in get_info(self,proxy,as_dict,*args,**kwargs)
414
415 def get_info(self,proxy=None,as_dict=False,**kwargs):
--> 416 self._get_fundamentals(proxy)
417 data = self._info
418 if as_dict:
/opt/anaconda3/lib/python3.7/site-packages/yfinance/base.py in _get_fundamentals(self,kind,proxy)
317 self._info.update(data[item])
318
--> 319 self._info['regularMarketPrice'] = self._info['regularMarketopen']
320 self._info['logo_url'] = ""
321 try:
KeyError: 'regularMarketopen'
解决方法
我通过粘贴解决了这个问题
url = "{}/{}".format(self._scrape_url,self.ticker)
以上
data = utils.get_json(url+'/financials',proxy)
在yfinance/base.py
。