You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用requests.get请求链接持续报IncompleteRead错误的解决方案咨询

解决requests.get触发IncompleteRead错误的方案

针对使用requests.get下载https://files.rcsb.org/header/6TAV.pdb时出现的IncompleteRead错误,推荐以下几种Python环境下的解决方法:

1. 配置请求重试策略

IncompleteRead错误常因网络传输中断或服务器临时响应异常导致,通过设置自动重试机制可有效解决:

import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# 创建会话并配置重试策略
session = requests.Session()
retry_strategy = Retry(
    total=3,  # 总重试次数
    backoff_factor=1,  # 重试间隔时间(秒),每次重试间隔翻倍
    status_forcelist=[429, 500, 502, 503, 504],  # 需要触发重试的状态码
    allowed_methods=["GET"]  # 仅对GET请求重试
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("https://", adapter)
session.mount("http://", adapter)

# 发起请求
response = session.get("https://files.rcsb.org/header/6TAV.pdb")
response.raise_for_status()  # 检查请求是否成功
content = response.content

2. 手动分块读取内容

即使开启stream=True,若直接读取完整内容仍出错,可尝试逐块读取:

import requests

url = "https://files.rcsb.org/header/6TAV.pdb"
response = requests.get(url, stream=True)
response.raise_for_status()

content = b""
# 按8KB块逐次读取
for chunk in response.iter_content(chunk_size=8192):
    if chunk:
        content += chunk

3. 模拟wget的请求头

服务器可能对不同客户端的请求做差异化处理,模拟wget的请求头可匹配成功下载的环境:

import requests

headers = {
    "User-Agent": "Wget/1.21.3 (linux-gnu)"
}
response = requests.get("https://files.rcsb.org/header/6TAV.pdb", headers=headers)
response.raise_for_status()
content = response.content

4. 封装wget调用(备选)

若上述方法均无效,可在Python中直接调用wget工具(需确保系统已安装wget):

import subprocess

# 下载到指定文件
subprocess.run(["wget", "-O", "6TAV.pdb", "https://files.rcsb.org/header/6TAV.pdb"], check=True)

内容的提问来源于stack exchange,提问作者fileyfood500

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 01:42:36