美文网首页
scrapy同时执行几个爬虫和顺序执行几个爬虫

scrapy同时执行几个爬虫和顺序执行几个爬虫

作者: 中v中 | 来源:发表于2021-09-14 20:23 被阅读0次

scrapy运行爬虫,除了常见的命令行,官方还提供了几种不同的运行方式,下面就来介绍一下。

从脚本运行Scrapy

您可以使用 API 从脚本运行Scrapy,而不是通过 scrapy crawl 运行Scrapy的典型方法.

请记住,Scrapy是建立在Twisted异步网络库之上的,因此您需要在Twisted reactor中运行它.

您可以用来运行爬虫的第一个实用程序是 scrapy.crawler.CrawlerProcess .该类将为您启动Twisted reactor,配置日志记录并设置关闭处理程序.此类是所有Scrapy命令使用的类.

这是一个示例,展示如何使用它运行单个爬虫.

import scrapy
from scrapy.crawler import CrawlerProcess

class MySpider(scrapy.Spider):
    # Your spider definition
    ...

process = CrawlerProcess({
    'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})

process.crawl(MySpider)
process.start() # the script will block here until the crawling is finished

请务必查看 CrawlerProcess 文档以熟悉其使用详细信息.

如果您在Scrapy项目中,则可以使用一些其他帮助程序在项目中导入这些组件.您可以自动导入您的名称传递给 CrawlerProcess 的爬虫,并使用 get_project_settings 获取项目设置的 Settings 实例.

以下是使用 testspiders 项目作为示例的工作示例.

from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

process = CrawlerProcess(get_project_settings())

# 'followall' is the name of one of the spiders of the project.
process.crawl('followall', domain='scrapinghub.com')
process.start() # the script will block here until the crawling is finished

还有另一个Scrapy实用程序可以更好地控制爬网过程: scrapy.crawler.CrawlerRunner .此类是一个简单包装程序,它封装了一些简单的帮助程序来运行多个爬虫程序,但它不会以任何方式启动或干扰现有的反应堆.

使用此类,应在调度爬虫后显式运行reacto。
如果您的应用程序已经在使用Twisted并且您想在同一个反应器中运行Scrapy,建议您使用CrawlerRunner而不是CrawlerProcess。
请注意,在爬虫完成后您还必须自己关闭Twisted反应器.这可以通过向 CrawlerRunner.crawl 方法返回的延迟添加回调来实现.

以下是其使用示例,以及在 MySpider运行完毕后手动停止反应堆的回调.

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

在同一进程中运行多个爬虫

默认情况下,当您运行 scrapy crawl 时,Scrapy会为每个进程运行一个爬虫.但是,Scrapy支持使用 internal API 为每个进程运行多个爬虫.

这是一个同时运行多个爬虫的示例:

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

使用 CrawlerRunner 的相同示例:

from twisted.internet import reactor
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging

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

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

configure_logging()
runner = CrawlerRunner()
runner.crawl(MySpider1)
runner.crawl(MySpider2)
d = runner.join()
d.addBoth(lambda _: reactor.stop())

reactor.run() # the script will block here until all crawling jobs are finished

相同的例子,但通过链接延迟顺序运行爬虫:

from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging

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

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

configure_logging()
runner = CrawlerRunner()

@defer.inlineCallbacks
def crawl():
    yield runner.crawl(MySpider1)
    yield runner.crawl(MySpider2)
    reactor.stop()

crawl()
reactor.run() # the script will block here until the last crawl call is finished```

相关文章

网友评论

      本文标题:scrapy同时执行几个爬虫和顺序执行几个爬虫

      本文链接:https://www.haomeiwen.com/subject/kxchgltx.html