下面所说的都是针对python2.7
复制代码 代码如下:
import chardet
line = "http://www.***.com"
html_1 = urllib2.urlopen(line,timeout=120).read()
encoding_dict = chardet.detect(html_1)
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')
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8