10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ScrapyをScriptから実行する際のスクリプト

Last updated at Posted at 2014-03-21

Run Scrapy from a script

Scrapyの公式ドキュメントにあるようなスクリプトだと、なぜかちゃんと動かなかった。
(クローリング対象は日本語のサイト)

spider = FollowAllSpider(domain='scrapinghub.com')
settings = get_project_settings()
crawler = Crawler(settings)
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run()

そこで、コマンドラインツール(scrapy crawl hogehoge)からの実行と同じように、Log周りの記述を修正したら動いた。

spider = FollowAllSpider(domain='scrapinghub.com')
settings = get_project_settings()
crawler = Crawler(settings)
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
log.start_from_crawler(crawler)
crawler.configure()
crawler.crawl(spider)
crawler.start()
reactor.run()

原因は、log.start()で実行すると、logがcrawlerを受け取らないからっぽい。
が、crawlerがないと動かない理由は不明。

scrapy/log.py
def start_from_crawler(crawler):
    return start_from_settings(crawler.settings, crawler)
10
7
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
10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?