您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python处理抓取中文编码和判断编码

2024/10/15 16:29:08发布36次查看
在开发自用爬虫过程中,有的网页是utf-8,有的是gb2312,有的是gbk,如果不加处理,采集到的都是乱码,解决的方法是将html处理成统一的utf-8编码
版本python2.7
#coding:utf-8 import chardet #抓取网页html line = http://www.pythontab.com html_1 = urllib2.urlopen(line,timeout=120).read() encoding_dict = chardet.detect(html_1) print encoding web_encoding = encoding_dict['encoding'] #处理,整个html就不会是乱码。 if web_encoding == 'utf-8' or web_encoding == 'utf-8': html = html_1 else : html = html_1.decode('gbk','ignore').encode('utf-8')
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product