【问题标题】:Catch exceptions from yfinance从 yfinance 捕获异常
【发布时间】:2021-02-28 02:38:06
【问题描述】:

我有以下代码:

try:
    yf.Ticker("AALB.NV").history(period = "3mo")
except:
    pass

这给出了例外: - AALB.NV:未找到数据,符号可能被除名 有什么办法可以捕捉到这个异常?

【问题讨论】:

    标签: algorithmic-trading yfinance


    【解决方案1】:

    如果符号无效,.history() 函数不会引发异常,它只会将该语句打印到您的屏幕上。您可以自己提出异常:

    import yfinance as yf
    
    try:
        data = yf.Ticker("AALB.NV").history(period="3mo")
    
        if 'Empty DataFrame' in str(data):
            raise Exception('Empty DataFrame - might be caused by an invalid symbol')
    
    except Exception as e:
        print(e)
    

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多