我在爬取凤凰网却出现
UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in position 151120: illegal multibyte sequence
这是我的代码
__author__ = 'my'
import urllib.request
url = 'http://www.ifeng.com/'
req = urllib.request.urlopen(url)
req = req.read()
req = req.decode('utf-8')
print(req)
为什么utf8却报错GBK?
这个是 cmd.exe 的问题,别的软件都能正确解码。例如 记事本、浏览器。。。。
追加:

其实也可以修改cmd.exe 的编码为 utf-8(cp65001)
步骤:
1、运行CMD.exe
2、chcp 65001
3、修改窗口属性的字体
在CMD窗口标题栏上点击右键,选择"属性"->"字体",将字体修改为True Type字体"Lucida Console"
如图:
4、运行 python

x.py 的内容: