在拒绝股票数据请求后继续查询雅虎财务

时间:2017-03-01 19:14:51

标签: python error-handling yahoo-api yahoo-finance stock

我正试图抢夺一些瑞典公司以及Python和雅虎财务的数据。当我到达KIND-SDB.ST {@ 1}}时,我会得到

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('SEK@close')
    numb+=1

numb = 1
for i in my_stocks:

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

1 个答案:

答案 0 :(得分: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('SEK@close')