如果没有网址爬网,请抓紧蜘蛛

拉海尔

我有一个蜘蛛,它从Redis列表中获取URL。

当找不到网址时,我想很好地关闭Spider。我尝试实现CloseSpider异常,但似乎还没有达到目的

def start_requests(self):
    while True:
        item = json.loads(self.__pop_queue())
        if not item:
            raise CloseSpider("Closing spider because no more urls to crawl")
        try:
            yield scrapy.http.Request(item['product_url'], meta={'item': item})
        except ValueError:
            continue

即使我提出了CloseSpider异常,但仍然出现以下错误:

root@355e42916706:/scrapper# scrapy crawl general -a country=my -a log=file
2017-07-17 12:05:13 [scrapy.core.engine] ERROR: Error while obtaining start requests
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/scrapy/core/engine.py", line 127, in _next_request
    request = next(slot.start_requests)
  File "/scrapper/scrapper/spiders/GeneralSpider.py", line 20, in start_requests
    item = json.loads(self.__pop_queue())
  File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

此外,我还尝试在同一函数中捕获TypeError,但它也不起作用。

有什么建议的方法来处理这个吗

谢谢

wer

您需要检查是否self.__pop_queue()返回了某些东西,然后再给予json.loads()(或TypeError在调用时捕获),例如:

def start_requests(self):
    while True:
        item = self.__pop_queue()
        if not item:
            raise CloseSpider("Closing spider because no more urls to crawl")
        try:
            item = json.loads(item)
            yield scrapy.http.Request(item['product_url'], meta={'item': item})
        except (ValueError, TypeError):  # just in case the 'item' is not a string or buffer
            continue

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Scrapy-爬网蜘蛛的流程

Wget爬网而没有下载文件

刮y的问题-没有爬网的物品

我如何抓紧接受一个充满URL的页面作为起始URL并仅对它们进行爬网?

删除文件(如果存在)。如果没有,请创建它

Praw Reddit Web爬网程序错误“对象没有属性”

使用BeautifulSoup的Python Web爬网AttributeError:'NoneType'对象没有属性'text'

在“ post_block hentry with_rep”类下没有返回结果的爬网代码

使用python scrapy进行动态网址爬网

Scrapy-自定义蜘蛛,尽管成功启动了爬虫,但零爬网

Ember Cli有多容易爬网?

Bootstrap AngularJS如果没有,请隐藏ul

PHP-如果没有结果,请重试

Ansible:检查文件文字,如果没有,请添加

WordPress:如果没有标题,请添加元标题

如果没有活动的窗口,请运行热键

如果没有“ ping”命令,请检查节点之间的连接

如果没有捕获任何内容,请取消语音识别

如果PHP数组中没有月份,请包含月份

如果没有值或null laravel,请不要更改值

如果没有先前的值,请替换该值

如果没有任何内容,请禁用选择

如果没有评论,请选择(获取)文章

如果没有https://网址,则无法链接到网站

AttributeError:“模块”对象没有属性“蜘蛛”

蜘蛛没有刮适量的物品

没有http,https的Yii2网址验证

Angular 7网址发生变化,但没有其他反应

Git-在“路径”没有检测到源目录。请检查网址,然后重试