python处理中文编码和判断编码示例

yipeiwu_com6年前Python基础

下面所说的都是针对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就不会是乱码。

相关文章

pytorch GAN伪造手写体mnist数据集方式

pytorch GAN伪造手写体mnist数据集方式

一,mnist数据集 形如上图的数字手写体就是mnist数据集。 二,GAN原理(生成对抗网络) GAN网络一共由两部分组成:一个是伪造器(Generator,简称G),一个是判别器(...

python解析xml文件实例分享

python解析xml文件实例分享

复制代码 代码如下:def get_area_list(self):        """获取地域省份和城市名称字典...

利用Python检测URL状态

需求:Python检测URL状态,并追加保存200的URL 代码一: #! /usr/bin/env python #coding=utf-8 import sys import...

python Tkinter版学生管理系统

python Tkinter版学生管理系统

本文实例为大家分享了python Tkinter版学生管理的具体代码,供大家参考,具体内容如下 Tkinter是python自带的UI包,无需下载,只需要导入 tkinter 文档 //...

在python的WEB框架Flask中使用多个配置文件的解决方法

有些框架本身就支持多配置文件,例如Ruby On Rails,nodejs下的expressjs。python下的Flask虽然本身支持配置文件管理, 但单纯使用from_object和...