Python3.6(windows系统)解决编码问题详解编程语言

Python3.6(windows系统)解决编码问题

1,py文件代码:

import urllib.request 
url = "http://www.douban.com/" 
webPage = urllib.request.urlopen(url) 
data = webPage.read() 
data = data.decode('UTF-8') 
print(data) 
print(type(webPage)) 
print(webPage.geturl()) 
print(webPage.info()) 
print(webPage.getcode())

2,执行出现字符编码异常:

python, 'gbk' codec can't encode character '/u2122' in position 42161: illegal multibyte sequence

3,解决方案:

#增加字符编码转换 
import sys, io 
# Change default encoding to utf8   
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') 

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16817.html

(0)
上一篇 2021年7月19日 19:38
下一篇 2021年7月19日 19:38

相关推荐

发表回复

登录后才能评论