Python deals with Chinese coding and judgment coding examples

  • 2020-04-02 13:29:27
  • OfStack

All of this is for python2.7


#coding:utf-8
#chardet  Need to download and install 
import chardet
# Scraping of the page 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')
# There's the above treatment, the whole thing html It won't be a garbled code. 


Related articles: