Yahoo Finance Python API JSON错误?

时间:2016-10-21 14:33:32

标签: python json python-3.x yahoo-finance

我一直在尝试使用Yahoo Finance Python API获取历史价格但我每次尝试运行代码时都会遇到同样的错误。这是我的代码和错误:

Python代码

from yahoo_finance import Share 

name = Share('YHOO') 
temp = (name.get_historical('2014-04-25','2015-04-29'))

print (temp)

错误信息

> 
> JSONDecodeError                           Traceback (most recent call
> last) <ipython-input-1-65dd835a5e8b> in <module>()
> ----> 1 from fetch import *
> 
> /Users/peterbullard/SMP/fetch.py in <module>()
>       3 
> ----> 4 name = Share('YHOO')
>       5 temp = (name.get_historical('2014-04-25', '2015-04-29'))
>       6 
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/yahoo_finance-1.2.1-py3.5.egg/yahoo_finance/__init__.py
> in __init__(self, symbol)
>     176         self._table = 'quotes'
>     177         self._key = 'symbol'
> --> 178         self.refresh()
>     179 
>     180     def _fetch(self):
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/yahoo_finance-1.2.1-py3.5.egg/yahoo_finance/__init__.py
> in refresh(self)
>     140 
>     141         """
> --> 142         self.data_set = self._fetch()
>     143 
>     144 
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/yahoo_finance-1.2.1-py3.5.egg/yahoo_finance/__init__.py
> in _fetch(self)
>     179 
>     180     def _fetch(self):
> --> 181         data = super(Share, self)._fetch()
>     182         if data['LastTradeDate'] and data['LastTradeTime']:
>     183             data[u'LastTradeDateTimeUTC'] = edt_to_utc('{0} {1}'.format(data['LastTradeDate'], data['LastTradeTime']))
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/yahoo_finance-1.2.1-py3.5.egg/yahoo_finance/__init__.py
> in _fetch(self)
>     132     def _fetch(self):
>     133         query = self._prepare_query(table=self._table, key=self._key)
> --> 134         data = self._request(query)
>     135         return data
>     136 
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/yahoo_finance-1.2.1-py3.5.egg/yahoo_finance/__init__.py
> in _request(self, query)
>     116 
>     117     def _request(self, query):
> --> 118         response = yql.YQLQuery().execute(query)
>     119         try:
>     120             _, results = response['query']['results'].popitem()
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/yahoo_finance-1.2.1-py3.5.egg/yahoo_finance/yql.py
> in execute(self, yql, token)
>      58 
>      59     self.connection.request('GET', PUBLIC_API_URL + '?' + urlencode({ 'q': yql, 'format': 'json', 'env': DATATABLES_URL }))
> ---> 60     return simplejson.loads(self.connection.getresponse().read())
>      61 
>      62   def __del__(self):
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/simplejson/__init__.py
> in loads(s, encoding, cls, object_hook, parse_float, parse_int,
> parse_constant, object_pairs_hook, use_decimal, **kw)
>     514             parse_constant is None and object_pairs_hook is None
>     515             and not use_decimal and not kw):
> --> 516         return _default_decoder.decode(s)
>     517     if cls is None:
>     518         cls = JSONDecoder
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/simplejson/decoder.py
> in decode(self, s, _w, _PY3)
>     368         if _PY3 and isinstance(s, binary_type):
>     369             s = s.decode(self.encoding)
> --> 370         obj, end = self.raw_decode(s)
>     371         end = _w(s, end).end()
>     372         if end != len(s):
> 
> /Users/peterbullard/anaconda/lib/python3.5/site-packages/simplejson/decoder.py
> in raw_decode(self, s, idx, _w, _PY3)
>     398             elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
>     399                 idx += 3
> --> 400         return self.scan_once(s, idx=_w(s, idx).end())
> 
> JSONDecodeError: Expecting value: line 1 column 1 (char 0)

如果有帮助,我正在使用Jupyter Notebook和Python 3.

这是来自终端的控制台日志。

  

W 08:59:40.681 NotebookApp]无法识别的JSON配置文件版本,   假设版本1 [I 08:59:41.410 NotebookApp] [nb_conda_kernels]   启用,找到2个内核[I 08:59:41.459 NotebookApp]端口8888是   已经在使用,尝试另一个端口。 [I 08:59:41.765 NotebookApp]   [nb_anacondacloud]已启用[I 08:59:41.768 NotebookApp] [nb_conda]   启用[I 08:59:41.821 NotebookApp]✓nbpresentHTML导出已启用   [W 08:59:41.821 NotebookApp]✗nbpresent PDF export DISABLED:No   模块名为&#39; nbbrowserpdf&#39; [I 08:59:41.825 NotebookApp]服务   本地目录中的笔记本:/ Users / peterbullard [I 08:59:41.825   NotebookApp] 0活动内核[I 08:59:41.825 NotebookApp]   Jupyter Notebook正在运行:http://localhost:8889/ [I 08:59:41.825   NotebookApp]使用Control-C停止此服务器并关闭所有服务器   内核(两次跳过确认)。 [I 08:59:53.346 NotebookApp]   内核开始:92b75861-94a5-4002-9ab4-97988c0cebfe [W 08:59:53.365   NotebookApp] 404 GET /kernelspecs/Python%20[Root]/logo-64x64.png(:1)   11.76ms referer = http://localhost:8889/notebooks/SMP/Test.ipynb [I 08:59:55.917 NotebookApp]内核重启:   92b75861-94a5-4002-9ab4-97988c0cebfe

1 个答案:

答案 0 :(得分:0)

我已按照https://pypi.python.org/pypi/yahoo-finance

的说明进行操作

它说:

  

雅虎!财务后端是http://datatables.org/。如果此服务已关闭或存在网络问题,您将收到来自组YQL *的错误,例如。 YQLQueryError。

所以服务已经结束,这就是你不断收到YQL错误的原因。如果这是2016年的一个问题,我现在假设该服务暂时没有服用。

对于修复,您可以尝试使用Yahoo Query Language(YQL)。这是开始https://developer.yahoo.com/yql/#python

的页面