如何处理金融中的“未找到”置顶器?

问题描述 投票:0回答:2

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

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

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

python python-3.x yahoo-finance
2个回答
0
投票
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.

0
投票

您可以尝试一个名为yahooquery的软件包。您实际上可以在检索任何数据之前验证符号:

from yahooquery import Ticker

list_of_tickers = [...]

tickers = Ticker(list_of_tickers)
valid_tickers = tickers.validation

# Keep only the valid tickers
tickers.symbols = [k for k, v in valid_tickers.items() if v]

# Then get your data
df = tickers.history(start=start_date)  # end is an argument and defaults to today
© www.soinside.com 2019 - 2024. All rights reserved.