python,抓取的网页为二进制乱码,怎么解决

如题所述

看看你的请求头Accept-Encoding是不是设置了gzip,deflate

这样的话,返回的response是需要解压缩的


# Content-Encoding: gzip
#Content-Encoding: deflate
if("Content-Encoding" in respInfo):
    if("gzip" == respInfo['Content-Encoding']):
        respHtml = zlib.decompress(respHtml, 16+zlib.MAX_WBITS);
    elif("deflate" == respInfo['Content-Encoding']):
        respHtml = zlib.decompress(respHtml, -zlib.MAX_WBITS);

温馨提示:答案为网友推荐,仅供参考