文章详情页
python - 编码问题求助
问题描述
爬虫时我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进行解码不就好了
相关文章:
1. js怎样实现在类似qq的联系人分组中进行搜索后,又可恢复原分组的界面?2. python - 如何获取rabbitmq连接的celery的个数3. 上传多图时,最后一张图为缩略图,想设置第一张图为缩略图怎么解决?4. python 中文乱码问题5. javascript - 百度地图一开始设置了中心点,怎么通过按钮在调转到对应的中心点????6. python - 如何将文本文件的CR换行符换成LF换行符?7. Mysql: FIND_IN_SET 的反运算8. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。9. mysql优化 - mysql 一张表如果不能确保字段列长度一致,是不是就不需要用到char。10. 怎样用python给mp3添加封面?
排行榜