【问题标题】:not able to make a DataFrame with yFinance JSON values无法使用 yFinance JSON 值创建 DataFrame
【发布时间】:2020-12-12 17:04:11
【问题描述】:

我正在尝试使用我从 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']),它运行良好并一一返回值。有人可以帮我解决这个问题吗?抱歉糟糕的总结,提前谢谢你。

【问题讨论】:

    标签: python api dataframe yfinance


    【解决方案1】:

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

    【讨论】:

    • 您好,非常感谢!我在 except 语句下添加了ector.append("null"),这样我就可以看到哪些股票没有价值。
    • 不客气!我推荐sector.append("null " + sym) 否则你的sector 列表只会显示"null" 缺少值,如果不添加sym,你就不会真正知道它是哪只股票。
    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多