Search code examples
pythonurllib

Download .xls file


I've been trying to download a .xls file using urllib like this

from urllib.request import urlretrieve as retrieve 
dls = "https://www.bvc.com.co/mercados/DescargaXlsServlet?archivo=acciones&fecha=2020-04-02&resultados=100&tipoMercado="
retrieve(dls,"Acciones.xls")

But I recieve a long error message starting with:

Traceback (most recent call last):
  File "C:\Users\quiki\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))

and ending with:

urllib.error.URLError: urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

I do not know if it has to do with the fact that the URL does not end with ".xls"


Solution

  • That's a ssl cert verification error. That means that the site's certificate is having some kind of problem.

    Try this code, it disables the verification.

    import requests
    
    dls = "https://www.bvc.com.co/mercados/DescargaXlsServlet?archivo=acciones&  fecha=2020-04-02&resultados=100&tipoMercado="
    
    with open("Acciones.xls","wb") as f:
        f.write(requests.get(dls,verify=False).content)
    

    Hope it helps.