python爬虫程序中:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte问题的原因及解决办法

import urllib.request
import urllib.parse

url="http://www.shuquge.com/txt/152/index.html"
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko',
}
with open('perfect_world.html','w',encoding='utf-8') as fp:
    fp.write(res.read().decode())

在执行上面的代码过程中,遇到问题:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte.

分析原因:

分析过程如下:

①:在上面代码中,headers请求头中,并不包括:Accept-encoding请求头,请求的响应内容理应是未经压缩的原生数据。通过fiddler抓包查看request头部信息。

抓包信息如下
GET http://www.shuquge.com/txt/152/index.html HTTP/1.1
Accept-Encoding: identity
Host: www.shuquge.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
Connection: close

从上面的请求头中我们看出Accept-Encoding:identify。在http协议中,Accept-Encoding:identify表示请求的响应数据是未压缩的。【如果请求头中没有"Accept-encoding"的头部信息,那么默认值为:Accept-Encoding:identify】排除request请求有误的原因。

②:分析response的响应内容。在fiddler抓包工具中,获取到了该request的响应。

由此找到了原因:响应数据是经过压缩之后的,由此造成了编码错误。压缩格式是Gzip格式,我们可以通过相应的算法进行解压。

然后问题又来了,为什么客户端请求的数据明明是未压缩的内容,而响应的数据是压缩过后的呢???

  有些服务器在实现http协议时可能存在bug,不支持identify或者请求头中并没有发送Accept-Encoding,那么服务器倾向于使用http1.0中的"gzip"and"compress",

扫描二维码关注公众号,回复: 4981068 查看本文章

服务器可能会按照客户端类型发送更合适的encoding类型。

  在我们的爬虫程序中,请求中并没有Accept-Encoding,但是在请求头中我们表明了自身是:Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko类型的客户端浏览器,所以服务器给我们发送的是gzip格式的数据,在上面的python程序中,并没有涉及到相关的解压操作,导致数据编码操作出错。

修复bug,完善程序

import urllib.request
import urllib.parse
import gzip

def ungzip(data):
    try:
        data=gzip.decompress(data)
    except:
        pass
    return data

url="http://www.shuquge.com/txt/152/index.html"
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko',
}

req=urllib.request.Request(url,headers=headers)
res=urllib.request.urlopen(req)

with open('perfect_world.html','w',encoding='utf-8') as fp:
    fp.write(ungzip( res.read()).decode())

代码成功运行,爬取结果。

猜你喜欢

转载自www.cnblogs.com/evil-smile/p/10290632.html