SoFunction
Updated on 2024-11-13

Example of python processing Chinese encoding and determining encoding

import chardet
#crawl web html
line = "http://www.***.com"
html_1 = (line,timeout=120).read()
#print html_1
encoding_dict = (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')