python爬虫html_网页爬虫 - 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'也不行一样报错,这个要怎么解决?

解决方案

"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)

扫一扫关注IT屋

微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值