[英]Yahoo Finance Python API JSON error?
我一直在嘗試使用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已在使用中,嘗試另一個港口。 [我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停止該服務器,並關閉所有的內核(兩次跳過確認)。 [我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
我按照這里的說明https://pypi.python.org/pypi/yahoo-finance
它說:
雅虎 財務后端是http://datatables.org/ 。 如果此服務已關閉或存在網絡問題,您將收到來自組YQL *的錯誤,例如。 YQLQueryError。
所以服務已經結束,這就是你不斷收到YQL錯誤的原因。 如果這是2016年的一個問題,我假設服務暫時沒有開啟。
要修復,您可以嘗試使用Yahoo Query Language(YQL)。 這是開始的頁面https://developer.yahoo.com/yql/#python
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.