スクリプトから Scrapy を実行する方法が以下のドキュメントに記載されていますが、その際、抽出したデータをcsvで出力する方法をご教授いただけますでしょうか。よくある例 — Scrapy 1.7.3 ドキュメント
from **ボールドテキスト**twisted.internet import reactor import scrapy from scrapy.crawler import CrawlerRunner from scrapy.utils.log import configure_logging class MySpider(scrapy.Spider): # Your spider definition ... configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'}) runner = CrawlerRunner() d = runner.crawl(MySpider) d.addBoth(lambda _: reactor.stop()) reactor.run() # the script will block here until the crawling is finished
■試したこと
下記リンクにある以下のコードを試しましたが、csvファイルは出力されたものの、データが格納されていませんでした。
Scrapyのクローリング処理を外部のスクリプトから実行する
from twisted.internet import reactor, defer from scrapy.crawler import CrawlerRunner from scrapy.utils.log import configure_logging from scrapy.utils.project import get_project_settings settings = get_project_settings() settings.set('FEED_URI', 'results/search_%(filename)s_%(rank)s.csv') configure_logging() runner = CrawlerRunner(settings) @defer.inlineCallbacks def crawl(): yield runner.crawl('search_args', query='python', rank=0, comment='test', filename='python') yield runner.crawl('search_args', query='ruby', rank=0, comment='test', filename='ruby') reactor.stop() crawl() reactor.run()
なお、scrapy crawl ○○ -o hello.csv のコマンドからは出力できています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。