了,上面那个回复按错发出去了,删不掉,这里继续
折腾了一下,应该是如 3 楼所说是 gzip 过的缘故。我使用:
html = gzip.GzipFile(fileobj=StringIO.StringIO(html), mode="r")
html = html.read().decode('gbk').encode('utf-8')
终于输出可读的内容了,编码也正确了。
于是,对于 gzip 解码又有了个疑问,就是为什么不能直接用 zlib.decompress() 来直接解压缩字符串,而非要通过 gzip 和 StringIO 麻烦兮兮地绕道呢?如果我使用:
html = zlib.decompress(html)
会输出错误:
zlib.error: Error -3 while decompressing data: incorrect header check
Google一下发现了:
http://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib有同学遇到类似的问题,也可以参考这里。
主贴中的问题算是解决了,非常感谢所有楼上的同学!