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

推荐订阅源

量子位
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
T
Threat Research - Cisco Blogs
C
Cisco Blogs
Recent Announcements
Recent Announcements
S
Securelist
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
P
Privacy & Cybersecurity Law Blog
宝玉的分享
宝玉的分享
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LINUX DO - 最新话题
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Spread Privacy
Spread Privacy
PCI Perspectives
PCI Perspectives
Project Zero
Project Zero
I
Intezer
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
MyScale Blog
MyScale Blog
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
T
Tenable Blog
Jina AI
Jina AI
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志

博客园 - 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 的自动解压功能,然后根据需要手动处理压缩数据。这样可以给你最大的灵活性来处理各种压缩格式的数据。