【问题标题】:how to run different scrapy projects in just one python scripts如何在一个python脚本中运行不同的scrapy项目
【发布时间】:2016-04-20 17:11:30
【问题描述】:

全部。我是 Scrapy 的新手,尝试在同一个脚本中调用多个蜘蛛项目时遇到了一些困难。我有三个 Scrapy 项目,它们分别抓取不同的网页并将数据存储到 SQLite 中。但我无法弄清楚如何使用一个 Python 脚本来管理项目。这是我尝试过的: 1.我从官方文档中看到使用CrawlerProcess,但是这段代码不能通过管道将数据存储到SQLite中。 下面是示例代码:

import scrapy
from scrapy.crawler import CrawlerProcess

class MySpider1(scrapy.Spider):
    # Your first spider definition
    ...

class MySpider2(scrapy.Spider):
    # Your second spider definition
    ...

process = CrawlerProcess()
process.crawl(MySpider1)
process.crawl(MySpider2)
process.start() # the script will block here until all crawling jobs are finished

2.我用了三个脚本调用

os.system("scrapy crawl myspider")

并用一个整体的脚本来导入这三个脚本,但是报错是这样的:

Unknown Command:crawl
Use "scrapy" to see available commands

现在我的问题是,如何在一个 python 脚本中调用三个不同的项目,而不使用“scrapy crawl XXX”。我真的很困惑。 提前谢谢你。

【问题讨论】:

  • "但是这段代码不能用管道将数据存入SQLite"从哪里来?您是否在脚本中启用了管道?
  • 我只是在setting.py中启用了它们,不知道如何在脚本中启用它们。在一个脚本文件中完成所有的抓取工作似乎很困难。现在我发现运行多个蜘蛛项目最有效的方法是使用 Scrapyd。感谢回复:-D

标签: python scrapy web-crawler


【解决方案1】:

Scrapyd 用于运行多个爬虫项目。但是有一个小问题。蜘蛛项目必须在命令行中停止,而我想在我的脚本中停止蜘蛛。

【讨论】:

    猜你喜欢
    • 2012-11-06
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2018-11-06
    相关资源
    最近更新 更多