python的字符问题?
import urllib.request
url='http://www.zhihu.com/'
response=urllib.request.urlopen(url).read()
print(response)
打印的是b'\x1f\x8b\x08\x00x\xfd\xb8U\x02\xff\xed<ks\xd4V\x96\xdf\xf3+4\x9a\xa9\xd4L\x15r\xeb\xfd \xd8S\xa9L\xb2\x9b\xa4fC6\xd9\xda\xcd\xa6R
的一些字符改为
response=response.decode('utf-8')
print(response)
报错UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
改为gbk格式后
response=response.decode('gbk','ignore')
print(response)
出现乱码的问题d麀抂噺侩Kyz
芙兰D门番
9 years, 12 months ago
Answers
这个网站返回的是一个被压缩后的网页,具体的解决方案 http://stackoverflow.com/questions/31730474/why-cant-i-decode-the-zhih...
_昙天_
answered 9 years, 12 months ago