https://blog.csdn.net/jim7424994/article/details/22675759
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')