乱码源于响应体真实编码与requests默认解码行为不一致;应弃用response.text,改用response.content配合手动decode或beautifulsoup显式指定from_encoding。

乱码不是编码没设对,而是你没搞清响应体的真实编码和 Python 解析时的默认行为差异。直接 response.text 读取中文,八成出问题。
为什么 response.text 会乱码?
Requests 默认用 response.encoding 解码响应体,但它会按 HTTP 头的 Content-Type(比如 text/html; charset=utf-8)或 BOM、HTML meta 标签推测编码。一旦服务器没发正确 charset,或页面 meta 写错,response.text 就崩了。
- 真实响应体是
gbk编码,但 headers 声称utf-8→response.text强行按 utf-8 解 → 乱码 - HTML 中
<meta charset="gb2312">锛
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!











