Python3 怎么将Unicode转中文,以及GBK乱码ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯
来源:互联网 发布:mynba2k18网络维护中 编辑:程序博客网 时间:2024/06/06 04:02
原理:
如果***type(text) is bytes***,那么text.decode('unicode_escape')
如果type(text) is str,
那么text.encode(‘latin1’).decode(‘unicode_escape’)
1. 案例:
*
#coding=utf-8import requests,re,json,tracebackfrom bs4 import BeautifulSoupdef 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-8import requests#共有6页,首页为空不为6for 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)
阅读全文
1 0
- Python3 怎么将Unicode转中文,以及GBK乱码ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯
- PHP 中文 转UNICODE, JS 将UNICODE转为GBK
- HttpURLConnection以及GBK转UTF-8中文部分乱码问题
- Python3 中文使用以及中文乱码问题
- Qt QString 中文 char* UTF-8 QByteArray QTextCodec unicode gb2312 GBK 乱码与转码问题
- [转]vim打开中文(gbk)乱码
- Java将Unicode转中文
- unicode转gbk,gbk转unicode CC++
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030(转)
- python3中文乱码问题
- GBK转unicode
- C#中文乱码解决:UTF8 转 UNICODE
- PHP实现unicode乱码转中文
- 【python字符编码汇总】GBK、UTF8、unicode以及中文之间的相互表示
- java GBK中文乱码问题
- unicode转GBK,GNK转unicode,解决FATFS中文码表占用ROM问题
- python3 把\u开头的unicode转中文,把str形态的unicode转中文
- JS将unicode码转中文方法
- 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
- openfire服务的安装(二)
- CF GYM 100741 A Queries
- Android对EditText做输入限制
- python使用递归、尾递归、循环三种方式实现斐波那契数列
- Python3 怎么将Unicode转中文,以及GBK乱码ÖйúÉÙÊýÃñ×åÌØÉ«´åÕ¯
- webrtc视频采集
- 猜数游戏-牛客三模
- muduo库源码分析(3):异常类
- java 时间处理
- Easy UI
- 2017 Multi-University Training Contest 2 solutions BY 电子科技大学
- python读取excel(Python处理xls)
- linux permission denied