2017-03-01 74 views
0

我試圖抓取一些瑞典公司以及與Python和雅虎財務有關的數據。當我到了一些股票,如KIND-SDB.ST我得到在股票數據請求被拒絕後繼續查詢雅虎財務

RemoteDataError: Unable to read URL: http://ichart.finance.yahoo.com/table.csv?a=1&ignore=.csv&s=KIND-SDB.ST&b=1&e=1&d=2&g=d&f=2017&c=2017 

如何讓雅虎財經搶奪這個數據,如果我不能,我怎麼有行情的列表的其餘部分繼續進行,即使一個是腐敗的?

我的小腳本是這樣的:

start = datetime.date(2017,2,1) 
end = datetime.date.today() 


my_stocks = ['NEPA.ST','EVO.ST','KIND-SDB.ST','EVO.ST','ATCO-B.ST','KINV-B.ST','NDA-SEK.ST','SAND.ST','GRNG.ST','JM.ST','SAAB-B.ST','STAR-B.ST','ASSA-B.ST','INVE-B.ST','INDU-C.ST','SKF-B.ST'] 

pylab.rcParams['figure.figsize'] = (10, 4) # Change the size of plots 



def stockplotter(ticker,start,end,numb): 
    stock = pdr.get_data_yahoo(ticker,start,end) 
    plt.figure(numb) 
    stock["Adj Close"].plot(grid = True) 
    plt.title(ticker) 
    plt.ylabel('[email protected]') 
    numb+=1 

numb = 1 
for i in my_stocks: 

    stockplotter(i, start, end, numb) 
    numb += 1 

回答

0

繼續查詢API引發錯誤後,你需要catch and handle例外。

def stockplotter(ticker,start,end,numb): 
    try: 
     stock = pdr.get_data_yahoo(ticker,start,end) 
    except RemoteDataError: 
     pass 

    plt.figure(numb) 
    stock["Adj Close"].plot(grid = True) 
    plt.title(ticker) 
    plt.ylabel('[email protected]')