无法生成具有yFinance JSON值的DataFrame



我正试图用从yFinance.info收到的一些信息制作一个数据帧;p 500股票符号,我用股票符号做了一个for循环来检索数据


for sym in symbol:
x=yf.Ticker(sym)
sector.append(x.info['forwardPE'])

然而,每次运行它时,它都会运行很长一段时间并返回此错误。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-c87646d48ecd> in <module>
12 for sym in symbol:
13     x=yf.Ticker(sym)
---> 14     sector.append(x.info['forwardPE'])
15 
~/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)
444 
445     def get_info(self, proxy=None, as_dict=False, *args, **kwargs):
--> 446         self._get_fundamentals(proxy)
447         data = self._info
448         if as_dict:
~/opt/anaconda3/lib/python3.7/site-packages/yfinance/base.py in _get_fundamentals(self, kind, proxy)
283         # holders
284         url = "{}/{}/holders".format(self._scrape_url, self.ticker)
--> 285         holders = _pd.read_html(url)
286 
287         if len(holders)>=3:
~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in read_html(io, match, flavor, header, index_col, skiprows, attrs, parse_dates, thousands, encoding, decimal, converters, na_values, keep_default_na, displayed_only)
1098         na_values=na_values,
1099         keep_default_na=keep_default_na,
-> 1100         displayed_only=displayed_only,
1101     )
~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
913             break
914     else:
--> 915         raise retained
916 
917     ret = []
~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
893 
894         try:
--> 895             tables = p.parse_tables()
896         except ValueError as caught:
897             # if `io` is an io-like object, check if it's seekable
~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in parse_tables(self)
211         list of parsed (header, body, footer) tuples from tables.
212         """
--> 213         tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
214         return (self._parse_thead_tbody_tfoot(table) for table in tables)
215 
~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py in _parse_tables(self, doc, match, attrs)
543 
544         if not tables:
--> 545             raise ValueError("No tables found")
546 
547         result = []
ValueError: No tables found

当我在没有附加的情况下执行时(例如,"x.info['forwardPE'](,它运行良好,并逐个返回值。有人能帮我解决这个问题吗?对糟糕的总结感到抱歉,并提前感谢您。

您可以将行放在try块中,并将错误except放在块中,以查看哪些符号工作不正常。由于您要处理500个股票行情,您可能会遇到不止一个异常,因此我建议使用广泛的except Exception语句,并使用traceback(可选(来获取有关错误的更多信息

import traceback
import yfinance as yf
symbol = ['TSLA', 'F', 'MNQ', 'MMM']
sector = []
for sym in symbol:
try:
x = yf.Ticker(sym)
sector.append(x.info['forwardPE'])
except Exception as error:
print()
print(f'{error} for symbol {sym}')
print(traceback.format_exc())

print(sector)

最新更新