下面所說的都是針對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就不會是亂碼。
新聞熱點
疑難解答