ホームページ >バックエンド開発 >Python チュートリアル >Python処理中国語エンコードと判定エンコード例
以下はすべて Python2.7 用です
import chardet
#Catch webpage 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' または web_encoding == ' UTF-8':
html = html_1
else :
html = html_1.decode('gbk','ignore').encode('utf-8')
#上記の処理により、html全体が文字化けすることはありません。