来自YahooFinance的价格数据错误:TypeError:timedelta秒组件的不受支持的类型:NoneType

时间:2018-10-18 09:23:28

标签: python error-handling yahoo-finance nonetype

我列出了约250个股票代码(例如'AAPL','IBM','QQQ'等),并尝试使用YahooFinancials从Yahoo Finance获取价格数据。

nr_periods = 333

date_N_days_ago = (datetime.now() - timedelta(days = nr_periods)).strftime('%Y-%m-%d')
date_today = datetime.now().strftime('%Y-%m-%d')

yahoo_financials = YahooFinancials(symbols_list)
hist_prices = yahoo_financials.get_historical_price_data(date_N_days_ago, date_today, 'daily')

此代码会导致错误消息:

TypeError: unsupported type for timedelta seconds component: NoneType

问题在于,我每次执行代码时都不会出现此错误消息,而是随机出现的。我不知道为什么。这是完整的堆栈跟踪

Traceback (most recent call last):
  File "testfile.py", line 273, in <module>
    updatePrices(logger)
  File "testfile.py", line 185, in updatePrices
    hist_prices = yahoo_financials.get_historical_price_data(date_N_days_ago, date_today, 'daily')
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 547, in get_historical_price_data
    return self.get_stock_data('history', hist_obj=hist_obj)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 428, in get_stock_data
    dict_ent = self._create_dict_ent(tick, statement_type, tech_type, report_name, hist_obj)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 364, in _create_dict_ent
    cleaned_re_data = self._clean_historical_data(re_data)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 270, in _clean_historical_data
    cleaned_date = self.format_date(v)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 111, in format_date
    form_date = str((datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=in_date)).date())
TypeError: unsupported type for timedelta seconds component: NoneType

1 个答案:

答案 0 :(得分:0)

这确实是一个缺陷。作者已修复: https://github.com/JECSand/yahoofinancials