惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
博客园 - Franky
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
量子位
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
V
V2EX
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
G
Google Developers Blog
B
Blog
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
小众软件
小众软件
博客园 - 【当耐特】
H
Help Net Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
博客园 - 叶小钗
Jina AI
Jina AI
Cloudbric
Cloudbric
N
Netflix TechBlog - Medium
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
I
Intezer
IT之家
IT之家
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
W
WeLiveSecurity
G
GRAHAM CLULEY
J
Java Code Geeks
H
Heimdal Security Blog
Cyberwarzone
Cyberwarzone
MyScale Blog
MyScale Blog
Latest news
Latest news
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
Martin Fowler
Martin Fowler
V
Visual Studio Blog
Webroot Blog
Webroot Blog
P
Palo Alto Networks Blog
T
Tor Project blog

博客园 - pcwanli

Python批量将Word文档(.doc)转换为.docx格式的完整实现步骤 Linux下版本控制器(SVN) -命令行客户端 linux svn 命令 Qwen3-VL视频科技:内容审核系统搭建 svn update 出现Skipped 'feifazuzhi' -- Node remains in conflict处理方法 linux redis.service如何编写 深入php redis pconnect PHP中使用Redis长连接笔记 python happybase 批量读取 如何用phpredis持久化连接pconnect方法提升应用响应速度 python处理常见格式压缩包文件的全指南 Python编程:happybase读写HBase数据库 python redis zset 按分值获取记录 hbase日志如何清理 hbase日志清理 Python的time.strftime()方法 python re.sub第二参数,前值如何引用 【Python】基于python实现Windows Service程序 python解析url参数 提取网页源码头信息的正则表达式 PHP字符串分割:explode()函数详解与应用 python importlib动态模块加载遇到is not a package错误,解决方法。 Python正则表达式替换(re.sub)的6种典型应用场景 用Python处理HTML转义字符的5种方式 pymongo批量更新bulk_write php mongodb操作 PHP 通过 Thrift 操作 Hbase 使用thrift的php版本操作hbase数据库 PHP通过Thrift操作Hbase
python requests get请求禁用自动解压
pcwanli · 2026-03-21 · via 博客园 - pcwanli

来源:百度搜索

在使用 Python 的 requests 库进行 HTTP GET 请求时,如果你遇到的问题是服务器返回了压缩编码(如 gzip 或 deflate)的数据,但是你希望直接接收原始数据而非解压缩的数据,你可以通过以下几种方式解决:

1. 禁用自动解压
requests 库默认会尝试自动解压响应内容。如果你不希望它自动解压,可以通过设置 decode_content 参数为 False 来实现。这样,你就可以接收到压缩的数据。

python
Copy Code
import requests

response = requests.get('http://example.com', stream=True)
# 禁用自动解压
response.raw.decode_content = False

# 现在你可以读取压缩的响应内容
content = response.raw.read()
print(content)
2. 手动处理压缩内容
如果你需要进一步处理压缩数据,比如你想要查看压缩后的数据或者对其进行某种分析,你可以在读取内容后手动解压。例如,使用 Python 的 zlib 或 gzip 模块来解压内容。

python
Copy Code
import requests
import zlib
import io

response = requests.get('http://example.com', stream=True)
# 禁用自动解压
response.raw.decode_content = False

# 读取压缩内容
compressed_content = response.raw.read()

# 使用 zlib 解压 gzip 压缩的数据
decompressed_content = zlib.decompress(compressed_content, zlib.MAX_WBITS | 16) # 对于 gzip 使用 16 + zlib.MAX_WBITS
print(decompressed_content)
3. 使用正确的 Content-Encoding 头部
如果你知道服务器使用的是哪种压缩方式(例如 gzip 或 deflate),你可以在请求中指定 Accept-Encoding 头部来告诉服务器你想要接收原始压缩格式的数据。但是,通常服务器会根据客户端请求的 Accept-Encoding 头部来决定是否压缩响应。如果你想要原始数据,最好是禁用自动解压。

python
Copy Code
import requests

headers = {'Accept-Encoding': 'identity'} # 请求不压缩数据,但服务器可能会忽略这个请求头
response = requests.get('http://example.com', headers=headers)
# 然后禁用自动解压,如上所示。
4. 使用第三方库处理特定压缩格式
对于特定的压缩格式(如 brotli),你可能需要使用专门的库来处理,例如 Brotli:

python
Copy Code
import requests
from brotli import decompress as brotli_decompress

response = requests.get('http://example.com', stream=True)
# 禁用自动解压
response.raw.decode_content = False
compressed_content = response.raw.read()
decompressed_content = brotli_decompress(compressed_content) # 使用 Brotli 解压
print(decompressed_content)
总之,最简单和最直接的方法是禁用 requests 的自动解压功能,然后根据需要手动处理压缩数据。这样可以给你最大的灵活性来处理各种压缩格式的数据。