Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tickers_sp500() ERROR: ssl.SSLCertVerificationError #106

Open
muriatic opened this issue May 1, 2023 · 1 comment
Open

tickers_sp500() ERROR: ssl.SSLCertVerificationError #106

muriatic opened this issue May 1, 2023 · 1 comment

Comments

@muriatic
Copy link

muriatic commented May 1, 2023

Code:

import yahoo_fin.stock_info as si
sp500 = si.tickers_sp500()

Error (path names shortened to ...) :

Traceback (most recent call last):
  File "...\Python311\Lib\urllib\request.py", line 1348, in do_open   
    h.request(req.get_method(), req.selector, req.data, headers,
  File "...\Python311\Lib\http\client.py", line 1282, in request      
    self._send_request(method, url, body, headers, encode_chunked)
  File "...\Python311\Lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "...\Python311\Lib\http\client.py", line 1277, in endheaders   
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "...\Python311\Lib\http\client.py", line 1037, in _send_output 
    self.send(msg)
  File "...\Python311\Lib\http\client.py", line 975, in send
    self.connect()
  File "...\Python311\Lib\http\client.py", line 1454, in connect      
    self.sock = self._context.wrap_socket(self.sock,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\ssl.py", line 1075, in _create
    self.do_handshake()
  File "...\Python311\Lib\ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "MYPROJECTPATH\main.py", line 6, in <module>
    sp500 = si.tickers_sp500()
            ^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\site-packages\yahoo_fin\stock_info.py", line 129, in tickers_sp500
    sp500 = pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\site-packages\pandas\io\html.py", line 1205, in read_html
    return _parse(
           ^^^^^^^
  File "...\Python311\Lib\site-packages\pandas\io\html.py", line 986, in _parse
    tables = p.parse_tables()
             ^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\site-packages\pandas\io\html.py", line 262, in parse_tables
    tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
                                ^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\site-packages\pandas\io\html.py", line 821, in _build_doc
    raise e
  File "...\Python311\Lib\site-packages\pandas\io\html.py", line 802, in _build_doc
    with urlopen(self.io) as f:
         ^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\site-packages\pandas\io\common.py", line 265, in urlopen
    return urllib.request.urlopen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\urllib\request.py", line 519, in open
    response = self._open(req, data)
               ^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\urllib\request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "...\Python311\Lib\urllib\request.py", line 1391, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\urllib\request.py", line 1351, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)>
@muriatic
Copy link
Author

muriatic commented May 2, 2023

Potential Solution

For the function tickers_sp500 in the stock_info.py file:

def tickers_sp500(include_company_data = False):
    '''Downloads list of tickers currently listed in the S&P 500 '''
    # get list of all S&P 500 stocks
    sp500 = pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0]
    sp500["Symbol"] = sp500["Symbol"].str.replace(".", "-", regex=True)

    if include_company_data:
        return sp500

    sp_tickers = sp500.Symbol.tolist()
    sp_tickers = sorted(sp_tickers)
    
    return sp_tickers

One could add this to the top of the file:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

This is what worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant