scrapy 我怎么从气流刀上打电话?

cczfrluj  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(98)

我的scrapy项目运行得很好,与“scrapy crawspider_1”命令。如何触发它(或调用scrapy命令)从气流匕首?

with DAG(<args>) as dag:
     scrapy_task = PythonOperator(
          task_id='scrapy',
          python_callable= ?)
     task_2 = ()
     task_3 = ()
   ....
scrapy_task >> [task_2, task_3, ...]
klsxnrf1

klsxnrf11#

使用BashOperator运行

with DAG(<args>) as dag:
     scrapy_task = BashOperator(
          task_id='scrapy',
          bash_command='scrapy crawl spider_1')
  • 如果您使用的是virtualenv,则可以使用VirtualEnvOperator
  • 或者使用现有环境,您可以使用source activate venv && scrapy crawl spider_1

使用Python运算符运行

  • 来自Scrapy文档:https://docs.scrapy.org/en/latest/topics/practices.html#run-scrapy-from-a-script
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

process = CrawlerProcess(get_project_settings())
process.crawl('spider_1')
process.start() # the script will block here until the crawling is finished

相关问题