UnicodeEncodeError: ‘gbk’ codec can’t encode character u’u2027′ in position 230 39: illegal multibyte sequence
完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13In [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试试:
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12In [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可以怎么解决,求教求教求教???????谢谢谢谢
我的第一个爬虫:。。。。