对服务器返回数据编码进行判断之chardet
by:授客 QQ:1033553122
测试环境
Win764Bit
chardet-2.3.0
下载地址1:https://pypi.python.org/pypi/chardet/
下载地址2:http://pan.baidu.com/s/1nu7XzjN
演示代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke' import urllib.request
import chardet
import urllib.parse if __name__ == '__main__':
params = urllib.parse.urlencode({"code":"utf-8","q":"手机", "callback":"cb"})
url = 'https://suggest.taobao.com/sug?' + params
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
response = response.read()
print(response)
encoding = chardet.detect(response)['encoding']
print('正在对服务器返回body进行解码')
if encoding == 'GB2312':
body = response.decode('gbk') # decode函数对获取的字节数据进行解码
print(body)
elif encoding == 'utf-8':
body = response.decode('utf-8')
print(body)
elif encoding == 'ascii':
body = response.decode('unicode_escape')
print(body)
else:
print('解码失败,未知编码:%s,不对body做任何解码' % encoding)
运行结果