Python 和雅虎金融怪异的“YQLQueryError(response['error']['description'])"获取历史 [英] Python and Yahoo finance weird "YQLQueryError(response['error']['description'])" get_historical

查看:21
本文介绍了Python 和雅虎金融怪异的“YQLQueryError(response['error']['description'])"获取历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python 和 yahoo-finance 包得到了一些非常奇怪的结果.当我对单个股票代码运行我的函数时,该函数执行正常.但是如果我把它们放在一个循环中(希望稍后扩展列表),我会从 YQL 中得到一些错误代码.有没有人遇到过类似的错误或者我错过了什么?请参阅下面的代码和输出.抱歉,Python 相对较新.感谢您的帮助!

I am getting some very weird results using Python and yahoo-finance package. When I run my function on individual stock symbols the function performs OK. But if I put them in a loop (hope later on to expand the list), I got some error codes from YQL. Have anyone encountered similar errors or did I miss anything? See the code below and outputs. Sorry relatively new to Python. Thanks for any help!

t1=m1('AAPL')
t2=m1('MSFT')

df1=['MSFT','APPL']
print(t1)
print(t2)


for stock in df1:
    print(type(stock))

for stock in df1:
    m1(stock)

         Adj_Close       Close        High         Low        Open Symbol  \
Date                                                                            
2016-01-04  102.612183  105.349998  105.370003  102.000000  102.610001   AAPL   
2016-01-05  100.040792  102.709999  105.849998  102.410004  105.750000   AAPL   
2016-01-06   98.083025  100.699997  102.370003   99.870003  100.559998   AAPL   
2016-01-07   93.943473   96.449997  100.129997   96.430000   98.680000   AAPL   

          Volume  price_gap    target  
Date                                       
2016-01-04  67649400        NaN  0.026703  
2016-01-05  55791000   0.003797 -0.028747  
2016-01-06  68457400  -0.020933  0.001392  
2016-01-07  81094400  -0.020060 -0.022598  
        Adj_Close      Close       High        Low       Open Symbol  \
Date                                                                       
2016-01-04  53.015032  54.799999  54.799999  53.389999  54.320000   MSFT   
2016-01-05  53.256889  55.049999  55.389999  54.540001  54.930000   MSFT   
2016-01-06  52.289462  54.049999  54.400002  53.639999  54.320000   MSFT   
2016-01-07  50.470697  52.169998  53.490002  52.070000  52.700001   MSFT   

          Volume  price_gap    target  
Date                                       
2016-01-04  53778000        NaN  0.008837  
2016-01-05  34079700   0.002372  0.002185  
2016-01-06  39518900  -0.013261 -0.004971  
2016-01-07  56564900  -0.024977 -0.010057  
<class 'str'>
<class 'str'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in                  _request(self, query)
119         try:
--> ====120             _, results = response['query']['results'].popitem()
121         except (KeyError, StopIteration, AttributeError):

AttributeError: 'NoneType' 对象没有属性 'popitem'

AttributeError: 'NoneType' object has no attribute 'popitem'

在处理上述异常的过程中,又发生了一个异常:

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in      _request(self, query)
122             try:
--> 123                 raise YQLQueryError(response['error']['description'])
124             except KeyError:

KeyError: 'error'

During handling of the above exception, another exception occurred:

YQLResponseMalformedError                 Traceback (most recent call last)
<ipython-input-144-fb031b50c461> in <module>()
 11 for stock in df1:
 12     print(type(stock))
 ---> 13     m1(stock)
 14 
 15 for stock in df1:

 <ipython-input-143-7724677970e7> in m1(msft, d1, d2)
  1 def m1(msft,d1='2016-1-4',d2='2016-1-7'):
  2     msft=Share(msft)
  ----> 3     msft_p = msft.get_historical(start_date=d1, end_date=d2)
  4     msft_prices=pd.DataFrame(msft_p)
  5            msft_prices[['Open','Close','Volume','High','Low','Adj_Close']]=msft_prices[['Open','Close','Volume','High','Low','Adj_Close']].apply(pd.to_numeric)

//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in    get_historical(self, start_date, end_date)
340             try:
341                 query = self._prepare_query(table='historicaldata',     startDate=s, endDate=e)

--> 342 结果 = self._request(query)第343话344 结果 = [结果]

--> 342 result = self._request(query) 343 if isinstance(result, dict): 344 result = [result]

//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in _request(self, query)
123                 raise YQLQueryError(response['error']['description'])
124             except KeyError:
--> 125                 raise YQLResponseMalformedError()
126         else:
127             if self._is_error_in_results(results):

YQLResponseMalformedError: Response malformed.

推荐答案

此功能似乎已停止使用:https://forums.yahoo.net/t5/Yahoo-Finance-help/ichart-not-working-in-my-app/td-p/253560

It looks like this functionality has been discontinued: https://forums.yahoo.net/t5/Yahoo-Finance-help/ichart-not-working-in-my-app/td-p/253560

这里有一个解决方法(直接从历史页面获取数据):

Here's a workaround (grab the data directly from the history pages):

import urllib2
from BeautifulSoup import BeautifulSoup as bs

def get_historical_data(name, number_of_days):
    data = []
    url = "https://finance.yahoo.com/quote/" + name + "/history/"
    rows = bs(urllib2.urlopen(url).read()).findAll('table')[0].tbody.findAll('tr')

    for each_row in rows:
        divs = each_row.findAll('td')
        if divs[1].span.text  != 'Dividend': #Ignore this row in the table
            #I'm only interested in 'Open' price; For other values, play with divs[1 - 5]
            data.append({'Date': divs[0].span.text, 'Open': float(divs[1].span.text.replace(',',''))})

    return data[:number_of_days]

#Test
for i in get_historical_data('amzn', 5):
    print i

输出:

{'Date': u'Jun 02, 2017', 'Open': 999.0}
{'Date': u'Jun 01, 2017', 'Open': 998.59}
{'Date': u'May 31, 2017', 'Open': 1000.0}
{'Date': u'May 30, 2017', 'Open': 996.51}
{'Date': u'May 26, 2017', 'Open': 995.0}

这篇关于Python 和雅虎金融怪异的“YQLQueryError(response['error']['description'])"获取历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆