Python中抓取网页时遇到数据无法解码的问题,如何判断和解决编码错误?
初学... 代码是这样:
from html.parser import HTMLParser
import urllib.request
import chardet
pars = HTMLParser()
home_url = “https://wallstreetcn.com/”
response = urllib.request.urlopen(home_url)
content = response.read()
encoding = chardet.detect(content)
pars.feed(content.decode(encoding[“encoding”],errors=“ignore”))
chrome 看网页 metadata 里面 charset 用的 utf-8,我这里无论直接用'utf-8' 还是检测编码,均无法正确解码,有点 response 根本就没给出正确数据的感觉。请教一下
Python中抓取网页时遇到数据无法解码的问题,如何判断和解决编码错误?
1f8b 开头。。。。gzip 压缩啊 最简单的
import gzip
gzip.decompress(content)
遇到网页解码问题,先别急着换库,核心是先确定网页实际编码,再用对应方式解码。直接上代码:
import requests
import chardet
from bs4 import BeautifulSoup
def fetch_with_auto_decode(url):
# 1. 先获取原始字节流
resp = requests.get(url)
raw_bytes = resp.content
# 2. 检测实际编码(比resp.encoding更准)
detected = chardet.detect(raw_bytes)
actual_encoding = detected['encoding']
confidence = detected['confidence']
print(f"检测到编码: {actual_encoding} (置信度: {confidence})")
print(f"requests认为的编码: {resp.encoding}")
# 3. 优先使用检测到的编码,失败则用备选
try:
if actual_encoding and confidence > 0.7:
html = raw_bytes.decode(actual_encoding, errors='replace')
else:
# 常见编码备选方案
for enc in ['utf-8', 'gbk', 'gb2312', 'latin-1']:
try:
html = raw_bytes.decode(enc)
print(f"使用备选编码成功: {enc}")
break
except UnicodeDecodeError:
continue
else:
html = raw_bytes.decode('utf-8', errors='ignore')
except Exception as e:
html = raw_bytes.decode('utf-8', errors='ignore')
print(f"解码异常,使用忽略错误方式: {e}")
return html
# 使用示例
url = "你的目标网址"
html_content = fetch_with_auto_decode(url)
# 用BeautifulSoup时指定编码
soup = BeautifulSoup(html_content, 'html.parser', from_encoding='utf-8')
关键点:
- 不要相信
resp.encoding- 它可能不准,特别是服务器配置错误时 - 先取
.content(字节)再解码,而不是.text(自动解码可能出错) - chardet检测+备选方案是稳妥做法
- 指定errors参数防止崩溃,
'replace'或'ignore'看需求
常见编码陷阱:
- 中文站:gbk/gb2312/utf-8混用
- 老旧网站:可能用latin-1或windows-1252
- 动态内容:AJAX返回的可能和页面声明不同
一句话建议:拿字节、测编码、备方案。
换一个 HTML 解析器,html.parser 的容错率不高,试试看 lxml
from bs4 import BeautifulSoup
standard_html = BeautifulSoup(content, ‘lxml’)
试试看这个~
Accept-Encoding:gzip, deflate, br
果然… 可是如果是其他的压缩什么的怎么办… 只能这样靠丰富的经验么…
也可以控制头 Accept-Encoding 让服务端返回没压缩过的数据
网页最多也就是 gzip 压缩 在复杂也复杂不到哪
import os
import requests
html = requests.get(‘https://wallstreetcn.com’).content.decode(‘utf-8’)
print(html)
os.system(“pause”)
是这个吗

