'SSL Error when trying to fetch stock data using pandas datareader

I am using the following code to fetch stock data from yahoo finance using pandas datareader.

I run the code using spyder / anaconda platfrom.

Moreover I have an ISP that uses a content filtering service which blocks the content because when i disable the service i can access the data. Upon talking to the ISP customer service, they told me that i have to download and install a certificate.

I have downloaded the certificate. How can i make anaconda aware of the certificate?

Any other ideas that can help me fetch the data without disabling the content filtering service long term?

Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.11.1 -- An enhanced Interactive Python.


from pandas_datareader import data
# First day
start_date = '2014-01-01'
# Last day
end_date = '2018-01-01'
# Call the function DataReader from the class data
goog_data = data.DataReader('GOOG', 'yahoo', start_date, end_date)


Traceback (most recent call last):

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py", line 485, in wrap_socket
    cnx.do_handshake()

  File "C:\ProgramData\Anaconda3\lib\site-packages\OpenSSL\SSL.py", line 1934, in do_handshake
    self._raise_ssl_error(self._ssl, result)

  File "C:\ProgramData\Anaconda3\lib\site-packages\OpenSSL\SSL.py", line 1671, in _raise_ssl_error
    _raise_current_error()

  File "C:\ProgramData\Anaconda3\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
    raise exception_type(errors)

Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 672, in urlopen
    chunked=chunked,

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 376, in _make_request
    self._validate_conn(conn)

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 994, in _validate_conn
    conn.connect()

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py", line 394, in connect
    ssl_context=context,

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 370, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py", line 491, in wrap_socket
    raise ssl.SSLError("bad handshake: %r" % e)

SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])",)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))

MaxRetryError: HTTPSConnectionPool(host='finance.yahoo.com', port=443): Max retries exceeded with url: /quote/GOOG/history?period1=1388541600&period2=1514858399&interval=1d&frequency=1d&filter=history (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-1-54640caabbf2>", line 7, in <module>
    goog_data = data.DataReader('GOOG', 'yahoo', start_date, end_date)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 208, in wrapper
    return func(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\data.py", line 387, in DataReader
    session=session,

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\base.py", line 251, in read
    df = self._read_one_data(self.url, params=self._get_params(self.symbols))

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py", line 153, in _read_one_data
    resp = self._get_response(url, params=params)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\base.py", line 155, in _get_response
    response = self.session.get(url, params=params, headers=headers)

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)

SSLError: HTTPSConnectionPool(host='finance.yahoo.com', port=443): 
Max retries exceeded with 
url: /quote/GOOG/history?period1=1388541600&period2=1514858399&interval=1d&frequency=1d&filter=history
 (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))


Solution 1:[1]

I have used Michael Zeng Solution to solve my problem in the following way:

I opened with notepad the file cacert.pem and the certificate file provided by my ISP and copied the missing certificate to the end of the cacert.pem

like in the screenshot after saving the file the code ran without errors

enter image description here

Solution 2:[2]

i did it the same way as Michael Zeng but i have used the cacert.pem file from site-packages\certifi

see here: https://appdividend.com/2022/01/29/python-certifi/

because python will use this cacert.pem file. Regards, Mehmet

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafael Zanzoori
Solution 2 Mehmet Sari