python3.x - Python3 bytes转str失败?要如何解决?
ringa_lee
ringa_lee 2017-04-17 17:57:26
0
4
640

我访问的网页明明是bytes代码

In [50]: type(html)
Out[50]: bytes

按照网上的方法,应该是这样转换的
str(html,"utf-8")
测试失败!
提示

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

然后又按照教程:
html.decode("UTF-8")

测试又失败
提示

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

然后也不知道错在哪里?
访问的是百度的源代码,是这样的结果:

不知道有高人指点一下?

ringa_lee
ringa_lee

ringa_lee

모든 응답(4)
Ty80

如果是requests获取的话,
可以尝试
req = requests.get(url)
req.encoding = "utf-8"
或者
req.encoding = req.apparent_encoding

Peter_Zhu

说明不是utf8

试试 gbk

伊谢尔伦

chr()函数

巴扎黑

如果是中文可以试试decode('gb18030')

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!