gpt4 book ai didi

python - 使用 pd.read_excel() 时,有没有办法解决 python 上的 ssl 错误(DH key 太小)?

转载 作者:行者123 更新时间:2023-12-04 22:42:26 26 4
gpt4 key购买 nike

我正在尝试读取可以通过 url 访问的 excel 表,并将其加载到 DataFrame 中。
我正在使用 pd.read_excel 像这样:


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)
我得到:
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)>
我在 this question 中看到了类似的东西但我试图在我的答案代码中包含无济于事。我对此有点陌生,但由于找不到解决方案,我认为最好在这里问。有没有办法解决这个 ssl 错误?

最佳答案

我再次尝试了链接中的解决方案并且它有效,但我不得不将一些代码更改为:

req = requests.get(url)
df = pd.read_excel(req.content,header=[2,3,4],nrows=347)
出于某种原因,添加请求使解决方案起作用。

关于python - 使用 pd.read_excel() 时,有没有办法解决 python 上的 ssl 错误(DH key 太小)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66052148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com