简体   繁体   English

Yahoo Finance Python API JSON错误?

[英]Yahoo Finance Python API JSON error?

I have been trying to use the Yahoo Finance Python API to get historical prices but I every time I try to run my code, I get the same error. 我一直在尝试使用Yahoo Finance Python API获取历史价格但我每次尝试运行代码时都会遇到同样的错误。 Here is my code and the error: 这是我的代码和错误:

The Python Code Python代码

from yahoo_finance import Share 

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

print (temp)

And the Error Message 和错误消息

> 
> 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)

If it helps, I am using Jupyter Notebook and Python 3. 如果它有帮助,我使用的是Jupyter Notebook和Python 3。

This is the console log from terminal. 这是终端的控制台日志。

W 08:59:40.681 NotebookApp] Unrecognized JSON config file version, assuming version 1 [I 08:59:41.410 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found [I 08:59:41.459 NotebookApp] The port 8888 is already in use, trying another port. 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] enabled [I 08:59:41.768 NotebookApp] [nb_conda] enabled [I 08:59:41.821 NotebookApp] ✓ nbpresent HTML export ENABLED [W 08:59:41.821 NotebookApp] ✗ nbpresent PDF export DISABLED: No module named 'nbbrowserpdf' [I 08:59:41.825 NotebookApp] Serving notebooks from local directory: /Users/peterbullard [I 08:59:41.825 NotebookApp] 0 active kernels [I 08:59:41.825 NotebookApp] The Jupyter Notebook is running at: http://localhost:8889/ [I 08:59:41.825 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [我08:59:41.765 NotebookApp] [nb_anacondacloud]启用[我08:59:41.768 NotebookApp] [nb_conda]启用[我08:59:41.821 NotebookApp]✓nbpresent HTML出口启用[W 08:59:41.821 NotebookApp]✗ nbpresent PDF导出禁用:无模块名为 'nbbrowserpdf'[我08:59:41.825 NotebookApp]服务的笔记本电脑从本地目录:/用户/ peterbullard [我08:59:41.825 NotebookApp] 0活跃的内核[我08:59:41.825 NotebookApp该Jupyter笔记本运行在: HTTP://本地主机:8889 / [我08:59:41.825 NotebookApp]使用Control-C停止该服务器,并关闭所有的内核(两次跳过确认)。 [I 08:59:53.346 NotebookApp] Kernel started: 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] Kernel restarted: 92b75861-94a5-4002-9ab4-97988c0cebfe [我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引荐= HTTP://本地主机:8889 /笔记本/ SMP / Test.ipynb [I 08:59:55.917 NotebookApp]内核重新启动:92b75861-94a5-4002-9ab4-97988c0cebfe

I've followed the instructions from here https://pypi.python.org/pypi/yahoo-finance 我按照这里的说明https://pypi.python.org/pypi/yahoo-finance

It says: 它说:

Yahoo! 雅虎 Finance backend is http://datatables.org/ . 财务后端是http://datatables.org/ If this service is down or has network problems you will receive errors from group YQL*, eg. 如果此服务已关闭或存在网络问题,您将收到来自组YQL *的错误,例如。 YQLQueryError. YQLQueryError。

So the service is out and that's why you keep getting a YQL error. 所以服务已经结束,这就是你不断收到YQL错误的原因。 I'm assuming the service hasn't been on for a while now if this was an issue back in 2016. 如果这是2016年的一个问题,我假设服务暂时没有开启。

For a fix you can try the Yahoo Query Language (YQL). 要修复,您可以尝试使用Yahoo Query Language(YQL)。 Here is the page to get started there https://developer.yahoo.com/yql/#python 这是开始的页面https://developer.yahoo.com/yql/#python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM