CentOS 环境使用代理下载数据失败-EOF occurred in violation of protocol (_ssl.c:1002)
我的代码如下,主要是通过yfinance包下载历史交易数据:
import yfinance as yf
data = yf.download('000001.SZ', start='2017-01-01', end='2019-01-01')
data
错误现象
Failed to get ticker '000001.SZ' reason: HTTPSConnectionPool(host='query1.finance.yahoo.com', port=443): Max retries exceeded with url: /v1/test/getcrumb (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1002)'))) [*********************100%***********************] 1 of 1 completed 1 Failed download: ['000001.SZ']: YFTzMissingError('$%ticker%: possibly delisted; no timezone found')
定位步骤
1. 错误码 1002 通常表示 SSL/TLS 握手失败。
2. 使用 下面的命令定位原因
openssl s_client -connect query1.finance.yahoo.com:443 -showcerts
看上去一切正常
3. 看一下是不是他说的no timezone found的原因
import os
os.environ['TZ']
执行下来发现:
KeyError Traceback (most recent call last) Cell In[16], line 2 1 import os ----> 2 os.environ['TZ'] File <frozen os>:679, in __getitem__(self, key) KeyError: 'TZ' 4.有点问题,设置了变量再试还是有问题
我又直接在服务器上试 wget 命令,感觉也不太行:
[root@localhost clash]# wget https://query1.finance.yahoo.com
--2024-11-25 16:32:01-- https://query1.finance.yahoo.com/
Connecting to 127.0.0.1:7890... connected.
Unable to establish SSL connection.
综上没有使用代理时会报错:
Failed to get ticker '000001.SZ' reason: HTTPSConnectionPool(host='query1.finance.yahoo.com', port=443): Max retries exceeded with url: /v1/test/getcrumb (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f93efef2f10>: Failed to establish a new connection: [Errno 111] Connection refused'))) [*********************100%***********************] 1 of 1 completed 1 Failed download: ['000001.SZ']: YFTzMissingError('$%ticker%: possibly delisted; no timezone found')
代理开启后:
Failed to get ticker '000001.SZ' reason: HTTPSConnectionPool(host='query1.finance.yahoo.com', port=443): Max retries exceeded with url: /v1/test/getcrumb (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1002)'))) [*********************100%***********************] 1 of 1 completed 1 Failed download: ['000001.SZ']: YFTzMissingError('$%ticker%: possibly delisted; no timezone found')
这误导了我,让我觉得代理很有问题,于是我各种尝试,反复调整代理设置,甚至是调整各种防火墙和包,都一无所获!
终于在半夜,当我下定决心不搞定不睡觉的时候这里找到了原因:
Python 遭遇 ProxyError 问题记录_Linux_开心洋葱网
解决办法就是,重新指定urllib3的版本,当前使用的最新版本在有代理的情况下不好用:
pip install urllib3==1.25.11
只能说我对Python太不熟悉了!!!