前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 编码转换与中文处理

Python 编码转换与中文处理

作者头像
py3study
发布2020-01-06 23:14:32
8430
发布2020-01-06 23:14:32
举报
文章被收录于专栏:python3python3

Python 编码转换与中文处理

python 中的 unicode是让人很困惑、比较难以理解的问题. utf-8是unicode的一种实现方式,unicode、gbk、gb2312是编码字符集.

decode是将普通字符串按照参数中的编码格式进行解析,然后生成对应的unicode对象

写python时遇到的中文编码问题:

代码语言:javascript
复制
➜  /test sudo vim test.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
def weather():
        import time
        import re
        import urllib2
        import itchat
        #模拟浏览器
        hearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        url = "https://tianqi.moji.com/weather/china/guangdong/shantou"    ##要爬去天气预报的网址
        par = '(<meta name="description" content=")(.*?)(">)'    ##正则匹配,匹配出网页内要的内容
        ##创建opener对象并设置为全局对象
        opener = urllib2.build_opener()
        opener.addheaders = [hearders]
        urllib2.install_opener(opener)
        ##获取网页
        html = urllib2.urlopen(url).read().decode("utf-8")
        ##提取需要爬取的内容
        data = re.search(par,html).group(2)
        print type(data)
        data.encode('gb2312')
        b = '天气预报'
        print type(b)
        c = b + '\n' + data
        print c
weather()
代码语言:javascript
复制
➜  /test sudo python test.py
<type 'unicode'>
<type 'str'>
Traceback (most recent call last):
  File "test.py", line 30, in <module>
    weather()
  File "test.py", line 28, in weather
    c = b + '\n' + data
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

解决方法:

代码语言:javascript
复制
➜  /test sudo vim test.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys
reload(sys)
# Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
sys.setdefaultencoding('utf-8')
def weather():
        import time
        import re
        import urllib2
        import itchat
        #模拟浏览器
        hearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        url = "https://tianqi.moji.com/weather/china/guangdong/shantou"    ##要爬去天气预报的网址
        par = '(<meta name="description" content=")(.*?)(">)'    ##正则匹配,匹配出网页内要的内容
        ##创建opener对象并设置为全局对象
        opener = urllib2.build_opener()
        opener.addheaders = [hearders]
        urllib2.install_opener(opener)
        ##获取网页
        html = urllib2.urlopen(url).read().decode("utf-8")
        ##提取需要爬取的内容
        data = re.search(par,html).group(2)
        print type(data)
        data.encode('gb2312')
        b = '天气预报'
        print type(b)
        c = b + '\n' + data
        print c
weather()

测试后:

代码语言:javascript
复制
➜  /test sudo python test.py
<type 'unicode'>
<type 'str'>

个人感觉网上说中文乱码通用解决办法都是错误的,因为类型不一样解决方法也不一样,所以最近刚好出现了这种问题,从网上找了很多办法没解决到,最后自己去查看资料,才发现需要对症下药。

这是一个抓取网页代码的python脚本

代码语言:javascript
复制
➜  /test sudo cat file.py
#!/usr/bin/python
#_*_ coding:UTF-8 _*_
import urllib,urllib2
import re
url = 'http://sports.sohu.com/nba.shtml' #抓取的url
par = '20180125.*\">(.*?)</a></li>'
req = urllib2.Request(url)
response = urllib2.urlopen(req).read()
#response = unicode(response,'GBK').encode('UTF-8')
print type(response)
print response

遇到的问题:

使用中文抓取中文网页时,print出来的中文会出现乱码

代码语言:javascript
复制
➜  /test sudo python file.py
special.wait({
itemspaceid : 99999,
form:"bigView",
adsrc : 200,
order : 1,
max_turn : 1,
spec :{
onBeforeRender: function(){
},
onAfterRender: function(){
},
isCloseBtn:true//�Ƿ��йرհ�ť
}
});

解决方法:

屏幕快照 2018-01-26 上午9.06.16.png
屏幕快照 2018-01-26 上午9.06.16.png

查看网页源代码发现charset=GBK的类型所以python中要进行类型转换

代码语言:javascript
复制
➜  /test sudo cat file.py
#!/usr/bin/python
#_*_ coding:UTF-8 _*_
import urllib,urllib2
import re
url = 'http://sports.sohu.com/nba.shtml' #抓取的url
par = '20180125.*\">(.*?)</a></li>'
req = urllib2.Request(url)
response = urllib2.urlopen(req).read()
response = unicode(response,'GBK').encode('UTF-8')
print type(response)
print response
代码语言:javascript
复制
➜  /test sudo python file.py
special.wait({
itemspaceid : 99999,
form:"bigView",
adsrc : 200,
order : 1,
max_turn : 1,
spec :{
onBeforeRender: function(){
},
onAfterRender: function(){
},
isCloseBtn:true//是否有关闭按钮
}
});

现在已经把中文乱码解决了

import json

#打印字典

dict = {'name': '张三'}

print json.dumps(dict, encoding="UTF-8", ensure_ascii=False) >>>{'name': '张三'}

#打印列表

list = [{'name': '张三'}]

print json.dumps(list, encoding="UTF-8", ensure_ascii=False) >>>[{'name': '张三'}]

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档