Search code examples
pythonyahoo-finance

Exception handling with Yahoo-finance


I am trying to scrape data using yfinance and have met a road block when trying to retrieve a ticker with no data, the error is - 7086.KL: No data found for this date range, symbol may be delisted.

How do I try catch this error? I've tried try catching it as seen in the code below but it still prints that error.

The code:

tickerdata = yf.Ticker("7086.KL")

try:
    history = tickerdata.history(start="2019-06-01", end="2020-05-01")
except ValueError as ve:
    print("Error")

Any advise on how to solve this?


Solution

  • I just had a look at the source code. It looks like they are indeed just printing the message. But they are also adding the error to a dictionary in the shared.py file. You can use this to check for errors:

    from yfinance import shared
    
    
    ticker = <ticker as string>
    tickerdata = yf.Ticker(ticker)
    
    history = tickerdata.history(start="2019-06-01", end="2020-05-01")
    error_message = shared._ERRORS[ticker]