手机
当前位置:查字典教程网 >脚本专栏 >python >python处理中文编码和判断编码示例
python处理中文编码和判断编码示例
摘要:下面所说的都是针对python2.7复制代码代码如下:#coding:utf-8#chardet需要下载安装importchardet#抓取...

下面所说的都是针对python2.7

复制代码 代码如下:

#coding:utf-8

#chardet 需要下载安装

import chardet

#抓取网页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')

#有以上处理,整个html就不会是乱码。

【python处理中文编码和判断编码示例】相关文章:

python控制台显示时钟的示例

python单链表实现代码实例

python实现爬虫下载漫画示例

python解析文件示例

python多线程http下载实现示例

python动态加载变量示例分享

python读文件逐行处理的示例代码分享

python抓取网页内容示例分享

python调用cmd复制文件代码分享

python中文乱码的解决方法

精品推荐
分类导航