python爬虫打印HTML问题

import urllib.request
import urllib.parse

page = 1
url = "http://www.qiushibaike.com/8hr/page/" + str(page)
headers = {

"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N)"

}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request).read()
html = response.decode("utf-8")
print(html)

运行后就报错误:
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 18194-18195: invalid continuation byte
把'utf-8'改成'GBK'也不行一样报错,这个要怎么解决?

阅读 5.7k
2 个回答

"User-Agent": "Mozilla/5.0 (windows 6.0)"

python3

import urllib.request
url = "http://www.qiushibaike.com/8hr/page/1"
headers = {
    #"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N)"
    "User-Agent": "Mozilla/5.0 (windows 6.0)"
}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
c = response.read()
h = c.decode('utf-8')
print(h)

python3 不需要转码jieguocode



我又尝试了以下。发现response.decode('UTF-8')就可以。是的大写!!!
图片描述
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题