python处理中文编码和判断编码示例
下面所说的都是针对python2.7
代码如下:
#coding:utf-8 #chardet 需要下载安装 import chardet #抓取网页html line = "http://www.***.com" html_1 = urllib2.urlopen(line,timeout=120).read() #print html_1 encoding_dict = chardet.detect(html_1) #print encoding web_encoding = encoding_dict['encoding'] if web_encoding == 'utf-8' or web_encoding == 'UTF-8': html = html_1 else : html = html_1.decode('gbk','ignore').encode('utf-8') #有以上处理,整个html就不会是乱码。
相关推荐
huavhuahua 2020-11-20
weiiron 2020-11-16
cakecc00 2020-11-15
千锋 2020-11-15
JakobHu 2020-11-14
guangcheng 2020-11-13
xirongxudlut 2020-11-10
solarLan 2020-11-09
pythonxuexi 2020-11-08
文山羊 2020-11-07
susmote 2020-11-07
wuShiJingZuo 2020-11-05
Pythonjeff远 2020-11-06
jacktangj 2020-11-04
lousir 2020-11-04
Noneyes 2020-11-10
ailxxiaoli 2020-11-16
chensen 2020-11-14
Nostalgiachild 2020-11-13