无法使用 pandas 网络数据 reader 从 google 财务获取数据:UnicodeDecodeError

Unable to get data from google finance using pandas web data reader: UnicodeDecodeError

使用 Pandas 网络数据读取器从 google 财务加载 SPY 数据时出错:

import pandas_datareader.data as web dfSPY = web.DataReader('SPY', 'google', start_date, end_date)

File "/home/user/Projects/pmb/dataaccess.py", line 63, in import_data dfSPY = web.DataReader('SPY', 'google', start_date, end_date) File "/home/user/anaconda3/envs/quant35/lib/python3.5/site-packages/pandas_datareader-0.5.0-py3.5.egg/pandas_datareader/data.py", line 137, in DataReader session=session).read() File "/home/user/anaconda3/envs/quant35/lib/python3.5/site-packages/pandas_datareader-0.5.0-py3.5.egg/pandas_datareader/base.py", line 181, in read params=self._get_params(self.symbols)) File "/home/user/anaconda3/envs/quant35/lib/python3.5/site-packages/pandas_datareader-0.5.0-py3.5.egg/pandas_datareader/base.py", line 79, in _read_one_data out = self._read_url_as_StringIO(url, params=params) File "/home/user/anaconda3/envs/quant35/lib/python3.5/site-packages/pandas_datareader-0.5.0-py3.5.egg/pandas_datareader/base.py", line 98, in _read_url_as_StringIO out.write(bytes_to_str(text)) File "/home/user/anaconda3/envs/quant35/lib/python3.5/site-packages/pandas/compat/init.py", line 73, in bytes_to_str return b.decode(encoding or 'utf-8')

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 34539: invalid continuation byte

这是一个未决问题,您可以在此处查看:https://github.com/pydata/pandas-datareader/issues/424

这就是我根据 github 的评论解决的方法:

检查 GoogleDailyReader.url() 是否在 pandas_datareader/google/daily.py returns 'http://www.google.com/finance/historical' 中。 如果是这样,请将其更改为“http://finance.google.com/finance/historical”。 (www -> 金融) 我昨天在 PyCharm 下载 pandas-datareader 时 GoogleDailyReader.url() 的 return 值为 'http://www.google.com/finance/historical'。不知道为什么。

测试它:

import pandas_datareader as pdr
c = pdr.google.daily.GoogleDailyReader()

c.url
# http://www.google.com/finance/historical'

pdr.__version__
# '0.5.0'