python中unicode编码转换为中文:
我们使用python中,遇到爬取网站情况,用到unicode编码,我们需要将它转换为中文,unicode编码转换为中文的方法有四种:使用unicode_escape 解码、使用encode()方法转换,再调用bytes.decode()转换为字符串形式、 使用json.loads 解码(为json 格式)、使用eval(遇到Unicode是通过requests在网上爬取的时候)。具体内容请看本文。
方法一:使用unicode_escape 解码:
unicode = b'\\u4f60\\u597d' |
方法二:使用encode()方法转换,再调用bytes.decode()转换为字符串形式:
s = r'\u4f60\u597d' |
方法三: 使用json.loads 解码(为json 格式):
str = '\u4eac\u4e1c\u653e\u517b\u7684\u722c\u866b' |
方法四:使用eval(遇到Unicode是通过requests在网上爬取的时候):
response = requests.get(url,headers=headers) |