首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python3 怎么将Unicode转中文,以及GBK乱码ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯

Python3 怎么将Unicode转中文,以及GBK乱码ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯

作者头像
学到老
发布于 2018-03-19 17:04:41
发布于 2018-03-19 17:04:41
3.6K0
举报

原理:

代码语言:javascript
复制
如果***type(text) is bytes***,
那么text.decode('unicode_escape')

如果type(text) is str, 那么text.encode(‘latin1’).decode(‘unicode_escape’)

1. 案例:

*

代码语言:javascript
复制
#coding=utf-8
import requests,re,json,traceback
from  bs4 import  BeautifulSoup

def qiushibaike():
    content = requests.get('http://baike.baidu.com/city/api/citylemmalist?type=0&cityId=360&offset=1&limit=60').content

    soup = BeautifulSoup(content, 'html.parser')
    print(soup.prettify())  #.decode("unicode_escape")
    #目前soup.prettify()为str
    new=soup.prettify().encode('latin-1').decode('unicode_escape')
    #.dencode('latin-1').encode('latin-1').decode('unicode_escape')

    print(new)


if __name__=='__main__':
    qiushibaike()

2. 结果对比:

另外爬取时,网站代码出现GBK无法编译python3,如出现如下:

ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯[6]

示例:

代码语言:javascript
复制
#coding=utf-8
import requests
#共有6页,首页为空不为6
for i in range(6):
    if i==0:
        url='http://www.tcmap.com.cn/list/zhongguoshaoshuminzutesecunzhai.html'

    else:
        url='http://www.tcmap.com.cn/list/zhongguoshaoshuminzutesecunzhai'+str(i)+'.html'
    response=requests.get(url)
    print(type(response))
   #如需成功编译,在.TEXT下面增加#号部分 
    html=response.text   #.encode('latin-1').decode('GBK')
    print(html)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 原理:
  • 1. 案例:
  • 2. 结果对比:
  • ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯[6]
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档