爬知乎回答中文乱码的问题
在测试知乎回答采集的时候发现中文乱码,请问是什么原因造成的?有什么办法解决吗?这是在Jason的文字:
这是代码输出的文字:
源代码:
import json
import requests
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36'}
url = "https://www.zhihu.com/api/v4/questions/310681355/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics&limit=5&offset=0&platform=desktop&sort_by=default"
req = requests.get(url,headers = headers)
target = json.loads(req.text)['data']['excerpt']
print(target)
你能直接看到源代码,里面就是添加了不少干扰字符 塔利班 发表于 2019-8-29 17:47
你能直接看到源代码,里面就是添加了不少干扰字符
是喔,看到了,请问有办法搞定它不?谢谢。 本帖最后由 快速收敛 于 2019-8-29 18:07 编辑
字的编码问题,target = json.loads(req.content.decode("utf-8"))['data']['excerpt']
网页可能不是utf-8编码,直接获取二进制源码,再转化成utf-8编码 strtmp='yourstring'.encode('raw_unicode_escape') #编码为bytes
strtmp=strtmp.decode('gbk')#解码
https://www.hiwendi.com/detail/15/
页:
[1]