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

时间:2022-05-04
本文章向大家介绍Python3 怎么将Unicode转中文,以及GBK乱码ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯,主要内容包括原理:、1. 案例:、2. 结果对比:、ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯[6]、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

原理:

如果***type(text) is bytes***,
那么text.decode('unicode_escape')

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

1. 案例:

*

#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]

示例:

#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)