解决python3 UnicodeEncodeError: 'g

2018-03-24  本文已影响0人  _sisi_

print()函数的局限就是Python默认编码的局限,因为系统是win7的,python的默认编码不是'utf-8',改一下python的默认编码成'utf-8'就行了


import io  

import sys  

import urllib.request  

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码  


res=urllib.request.urlopen('http://www.baidu.com')  

htmlBytes=res.read()  

print(htmlBytes.decode('utf-8'))  

···
# -- coding: utf-8 --

r.encoding = 'gbk'
···

上一篇 下一篇

猜你喜欢

热点阅读