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

相关推荐
IT成长日记2 小时前
【Docker基础】Docker数据持久化与卷(Volume)介绍
运维·docker·容器·数据持久化·volume·
热爱生活的猴子2 小时前
阿里云服务器正确配置 Docker 国内镜像的方法
服务器·阿里云·docker
物联网老王4 小时前
Ubuntu Linux Cursor 安装与使用一
linux·运维·ubuntu
艾伦_耶格宇6 小时前
【ACP】阿里云云计算高级运维工程师--ACP
运维·阿里云·云计算
一位摩羯座DBA6 小时前
Redhat&Centos挂载镜像
linux·运维·centos
计算机毕设定制辅导-无忧学长7 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
cui_win8 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
风清再凯8 小时前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
深圳安锐科技有限公司8 小时前
深圳安锐科技发布国内首款4G 索力仪!让斜拉桥索力自动化监测更精准高效
运维·安全·自动化·自动化监测·人工监测·桥梁监测·索力监测
猫头虎8 小时前
猫头虎 AI工具分享:一个网页抓取、结构化数据提取、网页爬取、浏览器自动化操作工具:Hyperbrowser MCP
运维·人工智能·gpt·开源·自动化·文心一言·ai编程