UnicodeEncodeError: 'gbk' codec can't encode character u'u2027' in position 230 39: illegal multibyte sequence
完整代码:
In [1]: import urllib2
In [2]: import re
In [3]: web = urllib2.urlopen('https://movie.douban.com/'>
In [4]: web = urllib2.urlopen('https://movie.douban.com/')
In [5]: neirong=web.read()
In [6]: jiangrenhua=neirong.decode('UTF-8')
In [7]: print jiangrenhua
根据网上的办法:
1.jiangrenhua=neirong.decode('UTF-8')改为jiangrenhua=neirong.decode('UTF-8','ignore')
2、更改python的默认编码为utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
输入sys.getdefaultencoding()查看
编码已经改了
但是继续输出print jiangrenhua
依然出现上面的编码错误,请问怎么解决
如下:
试试在第一行加上:# coding: utf-8
依然不行
只好用python3的ipython试试:
代码如下:
In [1]: import urllib.request
In [2]: import re
In [3]: web = urllib.request.urlopen('https://movie.douban.com/'>
In [5]: neirong=web.read()
In [6]: jiangrenhua=neirong.decode('UTF-8')
In [7]: print (jiangrenhua)
最后显示地抓取内容如下:
最后还是想问,上面的问题使用python2.7可以怎么解决,求教求教求教???????谢谢谢谢
我的第一个爬虫:。。。。
这个错误貌似是控制台输出字符串的时候,编码错了。你没必要再控制台把这个变量print出来的,一定要看的话你可以考虑写到文件里。
另外,我最新的win10快速预览版,Python 2.7.13,IPython 5.4.1 没有你的问题。
建议你早日转投python3