LoginSignup
13
4

More than 5 years have passed since last update.

scrapyでspiderの開始時、終了時に特定の処理を挟ませる方法

Last updated at Posted at 2017-02-07

spiderの開始時、終了時にフックさせるような関数を書く方法です。

以下の内容をプロジェクトの直下に配置します。

import scrapy

class SpiderHook(object):

    @classmethod
    def from_crawler(cls, crawler):
        ext = cls

        crawler.signals.connect(ext.spider_opened, signal=scrapy.signals.spider_opened)
        crawler.signals.connect(ext.spider_closed, signal=scrapy.signals.spider_closed)

        return ext

    def spider_opened(self, spider):
        # spider開始時の処理

    def spider_closed(self, spider):
        # spider終了時の処理

そして、settings.pyにこのクラスを読み込む設定を書きます。

EXTENSIONS = {
    '<project name>.<file name>. SpiderHook': 100,
}

参考:
https://doc.scrapy.org/en/latest/topics/extensions.html

13
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
4