简体   繁体   中英

Pandas read_csv fails to download CSV with SSL Error

Here is the code to reproduce it:

import pandas as pd
url = 'https://info.gesundheitsministerium.gv.at/data/timeline-faelle-bundeslaender.csv'
df = pd.read_csv(url)

it fails with the following traceback:

URLError: <urlopen error [SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1129)>

Here is a link to check the url. The download works from the same browser, if you embed the link on a markdown cell in Jupyter.

Any ideas to make this "just work"?

Update:

as per the question suggested by Florin C. below,

This solution solves the issue when downloading via requests:

import requests
import urllib3
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:@SECLEVEL=1'

requests.get(url)

It would be just a matter of forcing Pandas to to the same, somehow.

My Environment:

Python implementation: CPython
Python version       : 3.9.7
IPython version      : 7.28.0

requests  : 2.25.1
seaborn   : 0.11.2
json      : 2.0.9
numpy     : 1.20.3
plotly    : 5.4.0
matplotlib: 3.5.0
lightgbm  : 3.3.1
pandas    : 1.3.4

Watermark: 2.2.0

Here is a solution if you need to automate the process and don't want to have to download the csv first and then read from file.

import requests
import urllib3
import io
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:@SECLEVEL=1'

res = requests.get(url)
pd.read_csv(io.BytesIO(res.content), sep=';')

It should be noted that it may not be safe to change the default cyphers to SECLEVEL=1 at the OS level. But this temporary change should be ok.

Your code has no problems whatsoever. I do think there is a problem with the URL, I've tried the same code with other URLs and it worked, I also noticed that if you download that csv file and then read the file with pandas it should also work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM