【问题标题】:Skip over errors when looping throgh yahoo finance tickers with yfinance使用 yfinance 循环通过 yahoo 财务代码时跳过错误
【发布时间】:2022-11-11 23:18:42
【问题描述】:

我正在使用 Python 中的 yfinance 从 yahoo Finance 下载数据并循环浏览数百个代码,但随机出现一些代码错误,这会破坏整个过程。

有没有办法捕获异常并继续循环,但在出现错误时忽略那个“错误”代码,这样我就不必每次都从头开始?

这是我的代码

stockslist = pd.read_csv('KuCoins.csv')

combined = yf.download("SPY", start ="2022-01-01", end="2022-01-02")
 
for index, row in stockslist.iterrows():
   ticker = (row['ticker'])
  
   
   data = yf.download(ticker, start ="2022-03-01", end=currentDate)

这是我得到的错误:

** [100%**] 1 个中的 1 个已完成

1 下载失败:

  • KDON-USD:未找到数据,交易品种可能被退市 回溯(最近一次通话最后):

例外:输入都是 NaN **

【问题讨论】:

  • 您能否提供stockslist 的样本,以便我们重现错误?

标签: python yahoo-finance yfinance


【解决方案1】:

您可以使用 try-except 块来处理它。

for index, row in stockslist.iterrows():
    try:
        ticker = (row['ticker'])
        data = yf.download(ticker, start ="2022-03-01", end=currentDate)
    except Exception as e:
         print ("There is an issue with ticker: {} and we are passing it".format(ticker))
         pass

【讨论】:

    猜你喜欢
    • 2021-01-18
    • 2021-11-24
    • 2022-07-01
    • 2018-01-01
    • 2012-01-18
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    相关资源
    最近更新 更多