无法制作具有 yFinance JSON 值的 DataFrame

not able to make a DataFrame with yFinance JSON values

我正在尝试使用从 yFinance.info 收到的一些信息制作一个数据框。我有一个标准普尔 500 股票代码列表,我使用股票代码创建了一个 for 循环来检索数据


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

然而,每次我 运行 它,它 运行 很长一段时间并且 return 出现这个错误。

---------------------------------------------------------------------------
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']),它 运行 很好并且 return 一个一个地值。可以有人能帮我解决这个问题吗?很抱歉总结得太糟糕了,提前谢谢你。

您可以将行放在 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)