0

我正在尝试使用从 yFinance.info 收到的一些信息制作一个数据框。我有一个标准普尔 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']),它运行良好并一个一个返回值。有人可以帮我解决这个问题吗?对不起,可怕的总结和先感谢您。

4

1 回答 1

0

您可以将该行放在一个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)
于 2020-12-12T18:14:01.777 回答