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

看上去一切正常

  1. 看一下是不是他说的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(': 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_开心洋葱网](https://blog.75271.com/31701.html#respond "Python 遭遇 ProxyError 问题记录_Linux_开心洋葱网") 解决办法就是,重新指定urllib3的版本,当前使用的最新版本在有代理的情况下不好用: pip install urllib3==1.25.11 只能说我对Python太不熟悉了!!!

相关推荐
小杨互联网1 小时前
JAR逆向工程实战对比:传统工具 vs 自动化解决方案
运维·自动化·jar·jar自动逆向工具·jar逆向源码
想学好C++的oMen2 小时前
文件基础IO
linux·服务器
早睡冠军候选人3 小时前
Ansible学习----管理复杂的 Play 和 Playbook 内容
运维·学习·云原生·ansible
西西学代码3 小时前
Flutter---Stream
java·服务器·flutter
chase。4 小时前
关于 nvidia-smi: no devices were found 解决方案
服务器·数据库·postgresql
Robpubking6 小时前
AWS 中 S3 的 server-side encryption 解释与说明
运维·aws
陌路209 小时前
Linux 34TCP服务器多进程并发
linux·服务器·网络
爱喝矿泉水的猛男9 小时前
单周期Risc-V指令拆分与datapath绘制
运维·服务器·risc-v
科技块儿9 小时前
【IP】公有&私有IP地址?
服务器·网络协议·tcp/ip
hakukun9 小时前
docker避免每次sudo方法
运维·docker·容器