2017-03-31 32 views
3

我得到一些非常奇怪的結果使用Python和雅虎財務包。 當我在單個股票代碼上運行我的函數時,函數執行OK。但是如果我把它們放在一個循環中(希望稍後擴展列表),我從YQL中得到了一些錯誤代碼。有沒有人遇到類似的錯誤,或者我錯過了什麼?請參閱下面的代碼和輸出。很抱歉,對Python來說比較新。謝謝你的幫助!Python和雅虎財務奇怪「YQLQueryError(response ['error'] ['description'])」get_historical

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'

在處理上述例外的,另一個異常:

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 (查詢) 343 if isinstance(result,dict): 344結果= [結果]

//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. 
+0

也許yahoo-財務軟件包檢測到多個數據調用時禁用該功能? – user4224870

+0

[Yahoo財務ichart服務可用性]的可能重複(https://stackoverflow.com/questions/44040042/yahoo-finance-ichart-service-availability) –

回答

4

它看起來像這樣的功能已經停產: https://forums.yahoo.net/t5/Yahoo-Finance-help/ichart-not-working-in-my-app/td-p/253560

這裏有一個解決方法(直接從歷史記錄頁面獲取數據):

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}