【问题标题】:Scrapy: run a spider from python script and wait it until the end of its executionScrapy:从 python 脚本运行蜘蛛并等待它执行结束
【发布时间】:2017-10-11 02:27:34
【问题描述】:

我创建了一个具有这种结构的小型Scrapy 项目:

scrapyProject/
 ├── scrapy.cfg
 └── scrapyProject
    ├── __init__.py
    ├── items.py
    ├── pipelines.py
    ├── settings.py
    └── spiders
        ├── crawl_products.py
        └── __init__.py

crawl_products.py 包含蜘蛛 products_spider。要启动我正在使用的蜘蛛:

scrapy crawl products_spider

现在我想从另一个 python 脚本启动蜘蛛并等待它的执行结束。

如果它可以帮助:我要运行蜘蛛的另一个脚本是 django 视图

【问题讨论】:

    标签: python django scrapy scrapy-spider


    【解决方案1】:

    你可以在scrapy docs这个很好的解释中找到一半的解决方案

    但是,这是解决方案中更重要的部分,永远不要直接从 django 视图运行爬虫(也不要从其他 Web 框架)。

    拜托,我经常看到这种方式,这样做会阻止您的网络应用程序。结果,您的视图将遇到浏览器超时,并且在某些时候您的应用程序将无法处理其他请求。

    干净的解决方案是使用运行刮板的后台进程。一个很好的库是celery,这个话题已经在这里详细讨论过:Running Scrapy spiders in a Celery task

    【讨论】:

    • Celery 对 Django 来说太过分了。使用 django-q
    • Django-q 可能更轻量级,但在这种情况下 celery 具有完全在 django 之外运行的巨大优势。将 scrapy 刮板集成到 django 控制的东西中可能是一个巨大的痛苦 - 至少在我过去尝试的每个场合都是这样......
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多