您的位置:首页技术文章
文章详情页

python - 编码问题求助

【字号: 日期:2022-06-30 08:40:45浏览:51作者:猪猪

问题描述

爬虫时我get到的内容content是这样的 b{'title':'u5173u6ce8','card_type_name':'u5173u6ce8'}, 请教一下如何把编码转换成汉字我先查到一个方法 follow_text = follow.text.encode(’utf-8’).decode(’unicode-escape’), 第一个response可以这样转换,然而follow.text转换会出现’utf-8’ codec can’t encode characters in position 2809-2810,然后尝试改为text[:2809],不会报错,改为text[2811:]依然有错使用 print(follow.text[2800:2810])输出为’_image_url’,不知道为什么转换不成功想问一下这个是什么问题,或者有没有什么其他办法把我get到的内容转换一下

问题解答

回答1:

如果 follow.text本身是 str(unicode) 的话,encode(’utf8’) 回去没有任何意义,直接 decode(’unicode-escape’) 就行

如果 follow.text是 bytes 的话,那就可以选择先 decode(’utf8’) ,再 decode(’unicode-escape’)

(假设你问题中正确的编码为 utf8 )

回答2:

你直接使用json进行解码不就好了

标签: Python 编程
相关文章: