pythonsslyfinance

How to disable or ignore the SSL for the Yfinance package


Because I'm behind a firewall at the office, I get an SSL error when running yFinance package and I would like to disable the SSL when he pulls data from yahoo.

Code example:

# Load packages
import yfinance as yf
# Get data
df = yf.download('SPY', start='2000-01-01', end='2024-10-01')
# print
print(df)

The error:

Failed to get ticker 'SPY' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)')))
[*********************100%***********************]  1 of 1 completed

1 Failed download:
['SPY']: YFTzMissingError('$%ticker%: possibly delisted; no timezone found')
Empty DataFrame
Columns: [(Adj Close, SPY), (Close, SPY), (High, SPY), (Low, SPY), (Open, SPY), (Volume, SPY)]
Index: []

Does any body know a solution for this problem?


Solution

  • Looking at the documentation for Tickers download() it looks like you can replace the default requests session with your own. I believe something like the following may work:

    import requests 
    import yfinance as yf
        
    unsafe_session = requests.session()
    unsafe_session.verify = False
    
    # Get data
    df = yf.download('SPY', start='2000-01-01', end='2024-10-01', session=unsafe_session)
    # print
    print(df)
    

    I'm unable to test against the same scenario you are facing, but this code runs fine on my machine.