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

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 番茄的梦想

Debian防火墙的ufw的使用 Linux—nvm教程 LNMP一键安装包安装的mysql远程连接不上的问题 linux service文件格式 linux if命令 微软sdk及运行时下载地址 pip相关介绍 结构(位置)伪类选择器 如何清除mstsc记录 ubuntu开启远程桌面功能 使用Windows远程桌面工具来远程连接控制Ubuntu系统 缓存头Cache-Control的含义和使用 正则表达式 以A开头B结尾 取中间的内容 nginx 不带www的域名跳转www域名 IIS配置导入导出 CSS中hover选择器的使用详解 html 锚点三种实现方法 重置自增长id 如何解决Visual Studio2012 与此版本的Windows不兼容
解决python错误 UnicodeDecodeError: 'gb2312' codec can't decode byte 0x8b in position 1: illegal multibyt
番茄的梦想 · 2023-08-21 · via 博客园 - 番茄的梦想

报错的代码:

url= 'http://kaijiang.500.com/shtml/ssq/19001.shtml'
page =urllib.request.urlopen(url)
content = page.read().decode('gb2312')

报这个错的原因是获取到的网页内容是经过压缩了的,打开url可以看到请求head

Accept-Encoding:gzip, deflate

一种方式是请求时把Accept-Encoding设为空,这样的话网页数据未压缩,会相对比较大,增加网络传输时间

另一种方式就是对请求后的数据解压,显然这个方式处理会更快,如下

content = gzip.decompress(page.read()).decode('gb2312')

可是修改之后发现运行还是报错,这时候发现应该是因为网页中含有编码集之外的字符(网页内容指定charset是gb2312),就算加到更大范围还是报错(PS:汉字字符集范围 gb2312 < gbk < gb18030)

这时候选择忽略这些无法解码的字符

content = gzip.decompress(page.read()).decode('gb2312','ignore')

再运行代码,发现这个问题已经解决了