py请求网页出现codec can't decode byte 0x8b

from urllib.request import urlopen, urlcleanup, Request
url = 'http://tech.qq.com/a/20181210...'
request = Request(url)
response = urlopen(request)
content = response.read().decode('gb2312')
urlcleanup()

出现:
UnicodeDecodeError: 'gb2312' codec can't decode byte 0x8b in position 1: illegal multibyte sequence

这个问题怎么解决呢!?网上我看了很多资料,都是和请求头
“'Accept-Encoding': 'gzip, deflate'” 这个有关,但是试了没有效果。

请帮忙解决一下~~~~
谢谢!!

阅读 3.6k
3 个回答

直接response.read().decode()

content = response.read().decode('gb2312',errors='ignore')

In [1]: from urllib.request import urlopen, urlcleanup, Request

In [2]: url = 'http://tech.qq.com/a/20181210/004750.htm'

In [3]: request = Request(url)

In [4]: response = urlopen(request)

In [5]: content = response.read().decode('gb2312')

In [6]: urlcleanup()

In [7]: content
Out[7]: '<!DOCTYPE html><html lang="zh-CN">

没有任何改变,可能是终端的编码问题吧~
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题