1

I am trying to read an excel table that can be accessed through an url, and load it into a DataFrame.

I'm using pd.read_excel like so:


url = "https://www.recope.go.cr/wp-content/uploads/2020/09/PRECIOS-HIST%C3%93RICOS-CONSUMIDOR-FINAL-1-2.xls"
df = pd.read_excel(url,header=[2,3,4],nrows=347)

and I get :

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/urllib/request.py", line 1350, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/local/lib/python3.7/http/client.py", line 1277, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.7/http/client.py", line 1323, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.7/http/client.py", line 1272, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.7/http/client.py", line 1032, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.7/http/client.py", line 972, in send
    self.connect()
  File "/usr/local/lib/python3.7/http/client.py", line 1447, in connect
    server_hostname=server_hostname)
  File "/usr/local/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/usr/local/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/usr/local/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1091)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 1063, in __init__
    path=str(self._io), storage_options=storage_options
  File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 939, in inspect_excel_format
    content_or_path, "rb", storage_options=storage_options, is_text=False
  File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 563, in get_handle
    storage_options=storage_options,
  File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 288, in _get_filepath_or_buffer
    req = urlopen(filepath_or_buffer)
  File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 194, in urlopen
    return urllib.request.urlopen(*args, **kwargs)
  File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 1393, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/local/lib/python3.7/urllib/request.py", line 1352, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1091)>

I saw something similar in this question but I tried to include the answer code in mine to no avail. I'm a bit new to this but as I couldn't find a solution I thought best to ask here. Is there any way to get around this ssl error?

1 Answers1

0

I tried the solution from the link again and it worked, but I had to change some of the code to:

req = requests.get(url)
df = pd.read_excel(req.content,header=[2,3,4],nrows=347)

For some reason, adding requests made the solution work.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I used the original code and it worked for me. It could be a cipher set configuration. Check it [here](https://stackoverflow.com/questions/38015537/python-requests-exceptions-sslerror-dh-key-too-small) – Paulo Marques Feb 04 '21 at 19:37
  • Hi @PauloMarques, The link you sent is exactly the one I was refering to in the end of my question. As I mentioned, that solution alone did not work, I also had to add the code on my answer (with requests), and then it worked. – Matheus Vasconcellos Cortezi Feb 12 '21 at 12:32
  • What I meant is that I used this `pd.read_excel(url,header=[2,3,4],nrows=347)` and it worked for me. – Paulo Marques Feb 12 '21 at 17:19