为了账号安全,请及时绑定邮箱和手机立即绑定
关注
HamsterZ

执行douban_spider出现问题

"UnicodeEncodeError",python3编码是utf-8,尝试用virtualenv解决:

(project_1) [root@hamster douban]# python3 -m scrapy crawl douban_spider

...

2018-08-28 22:59:33 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://movie.douban.com/top250> (referer: None)

2018-08-28 22:59:33 [scrapy.core.scraper] ERROR: Spider error processing <GET https://movie.douban.com/top250> (referer: None)

Traceback (most recent call last):

  File "/root/project_1/lib/python3.6/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks

    current.result = callback(current.result, *args, **kw)

  File "/root/douban/douban/spiders/douban_spider.py", line 14, in parse

    print(response.text)

UnicodeEncodeError: 'ascii' codec can't encode characters in position 349-352: ordinal not in range(128)

...

2018-08-28 22:59:33 [scrapy.core.engine] INFO: Spider closed (finished)

还是报错,检查了一下:

(project_1) [root@hamster douban]# python3

Python 3.6.5 (default, Aug 28 2018, 22:34:18) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> sys.getdefaultencoding()

'utf-8'

怀疑是"douban_spider.py"代码有误,但不知道错在哪,希望老师帮我解答一下,谢谢!?

最佳回答

2018-09-01

你用virtualenv的时候,运行文件时,是否workon切换到正确的解释器了

HamsterZ (提问者)

我发现好像是print(response.text)的问题,我跟着课程继续往下做,可以成功爬取并保存数据。
#1 2018-09-04

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信