I've deployed my Scrapy spiders on Heroku and they are running well but I have to launch them manually via the command line tool.
I would like to automate this process with a custom clock process but I'm struggling to write the clock.py file that would trigger the whole scraping.
I would like my spiders to start crawling at the same time (once a day). From the scrapy documentation I found two strategies that could work for me:
- Run multiple spiders simultaneously with the
CrawlProcess()or theCrawlerRunner() - Run the spiders sequentially with the
CrawlerRunner()
I have honestly no idea which one to use so I'll give it a try to run them sequentially.
This is what I have in my clock.py file:
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from twisted.internet import reactor, defer
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging
from apscheduler.schedulers.twisted import TwistedScheduler
import pytz
from bike_price.spiders.a4en1 import A4en1Spider
from bike_price.spiders.lepoledeloccasion import LepoledeloccasionSpider
configure_logging()
runner = CrawlerRunner(get_project_settings())
@defer.inlineCallbacks
def crawl():
yield runner.crawl(A4en1Spider)
yield runner.crawl(LepoledeloccasionSpider)
reactor.stop()
This is running perfectly:
if __name__ == '__main__':
crawl()
reactor.run()
But when I'm trying to implement the apscheduler like below then it's not working as expected:
if __name__ == '__main__':
scheduler = TwistedScheduler(timezone=pytz.timezone('Europe/Paris'))
scheduler.add_job(crawl, trigger='cron', hour='17', minute='49')
scheduler.start()
reactor.run()
It seems like I'm scraping nothing:
...
2021-11-04 17:49:00 [scrapy.core.engine] INFO: Spider opened
2021-11-04 17:49:00 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2021-11-04 17:49:00 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2021-11-04 17:49:00 [apscheduler.executors.default] INFO: Job "crawl (trigger: cron[hour='17', minute='49'], next run at: 2021-11-05 17:49:00 CET)" executed successfully
Any ideas or explanations?
Thanks!