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太不熟悉了!!!

相关推荐
JuiceFS1 天前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
chen9451 天前
mysql 3节点mgr集群部署
运维·后端
LH_R1 天前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler1 天前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
少妇的美梦2 天前
logstash教程
运维
chen9452 天前
k8s集群部署vector日志采集器
运维
chen9452 天前
aws ec2部署harbor,使用s3存储
运维
christine-rr2 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
東雪蓮☆2 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220892 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs