0

所以我试图在一个循环中获取一些股票数据(不确定我是否可以传递一个数组),如下所示:

def getData(ticker):
    print (ticker)
    data = pdr.get_data_yahoo(ticker, start=start_date, end=today)
    dataname= ticker+'_'+str(today)
    files.append(dataname)
    SaveData(data, dataname)

但是由于某些原因,我提供的一些代码pdr.get_data_yahoo()没有找到,python 抛出这个错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py", line 157, in _read_one_data
    data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
KeyError: 'HistoricalPriceStore'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "borsdata_api.py", line 65, in <module>
    getData(row['ticker'])
  File "borsdata_api.py", line 47, in getData
    data = pdr.get_data_yahoo(ticker, start=start_date, end=today)
  File "/usr/local/lib/python3.7/site-packages/pandas_datareader/data.py", line 82, in get_data_yahoo
    return YahooDailyReader(*args, **kwargs).read()
  File "/usr/local/lib/python3.7/site-packages/pandas_datareader/base.py", line 251, in read
    df = self._read_one_data(self.url, params=self._get_params(self.symbols))
  File "/usr/local/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py", line 160, in _read_one_data
    raise RemoteDataError(msg.format(symbol, self.__class__.__name__))
pandas_datareader._utils.RemoteDataError: No data fetched for symbol ADDV-TO-1.ST using YahooDailyReader

是否可以跳过此迭代并继续列表中的下一个迭代?

4

1 回答 1

-1
def getData(ticker):
    print (ticker)
    try:
        data = pdr.get_data_yahoo(ticker, start=start_date, end=today)
        dataname= ticker+'_'+str(today)
        files.append(dataname)
        SaveData(data, dataname)
    except:
        pass #or traceback.print_exc(), or traceback.format_exc()
        #print_exc() will raise the error and print traceback.
        #format_exc() will return the error as a string.
于 2020-04-29T14:15:31.993 回答