from urllib.request import urlopen
import chardet
response=urlopen(url,timeout=3)
html_byte=response.read()
chardit1 = chardet.detect(html_byte)
file = open(PROJECT_NAME + '/' + str(ALLNUM) + '.html', 'wb') html_string=html_byte.decode(chardit1['encoding']).encode('utf-8')
file.write(html_string)
file.close()
利用到了chardet中的detect方法,获取chardit1[‘encoding’]探知是何种类型的编码,对其进行译码,再编码。