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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM