[python爬虫]对html解析读取编码格式,统一转码为utf-8

来源:互联网 发布:windows loader 2.4 编辑:程序博客网 时间:2024/06/07 18:52
from urllib.request import urlopenimport  chardetresponse=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’]探知是何种类型的编码,对其进行译码,再编码。

0 0
原创粉丝点击