我列出了约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