【发布时间】:2018-10-18 09:23:27
【问题描述】:
我有一个包含大约 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
【问题讨论】:
-
完整的堆栈跟踪会有所帮助。哪一行引发了 TypeError?
-
我尝试使用 try - except 块来捕获错误,但即使这样也是不可能的。我的代码立即停止,并在 linux 终端中显示错误日志,而不是在我的日志文件中。
-
尝试在遇到错误时记录您的 date_N_days_ago 和 date_today。要么是 yahoo_financials 函数有问题,要么是你发送的数据有问题。
-
我确实记录了 date_N_days_ago 和 date_today。他们不会改变。我已经检查过了。这很奇怪,因为一个小时前我运行了脚本 - 很好,然后再次 - 很好,但我第三次开始(根本没有改变)它像描述的那样崩溃了。
-
在我看来,YahooFinancials 存在缺陷。可能想在他们的 git 项目中向他们报告。
标签: python error-handling yahoo-finance nonetype