【问题标题】:Shutting Down a specific crawler of 3 working crawlers in Crawler4j?在 Crawler4j 中关闭 3 个工作爬虫的特定爬虫?
【发布时间】:2019-12-12 08:15:26
【问题描述】:

我有多个工作爬虫一起运行

例如。

-爬虫1

-爬虫2

-爬虫3

我的问题是:如果我只想关闭 2 号爬虫怎么办?

我想 crawler4j 中的每个爬虫都有一个会话 ID,我可以 在请求其 ID 时将其关闭

我该如何实施?

编辑

我知道如何关闭正在工作的爬虫,但我的问题是……如果我 有用户的爬行系统,我希望每个用户都有自己的 爬虫,如果用户 x 想要关闭它的爬虫 .so,爬虫 用户 x 关闭而没有反映和关闭用户 y 爬虫

【问题讨论】:

标签: java web-scraping web-crawler crawler4j


【解决方案1】:

您必须将您的爬虫包装在 CrawlController 实例中:

CrawlController controller = new CrawlController(config,..);
controller.startNonBlocking(BasicCrawler.class, numberOfCrawlers);

Thread.sleep(30 * 1000);
controller.shutdown(); // shutdown crawling
controller.waitUntilFinish();

你会发现完整的例子here

更新
示例代码,Usercontroller 实例:

public class UserCreator {
  public User createNewUser() {
    CrawlController controller = new CrawlController(config,..);
    controller.startNonBlocking(BasicCrawler.class, numberOfCrawlers);

    return new User(controller);
  }
}

public class User {
  private CrawlController controller; 

  public User(CrawlController controller) {
    this.controller = controller;
  }

  public void shutdownCrawler() {
    controller.shutdown(); // shutdown crawling
    controller.waitUntilFinish();
  }
}

【讨论】:

  • 你好@sudipn,我知道,但我的问题是..如果我有用户的爬网系统,我希望每个用户都有它的爬虫,如果用户 x 想关闭它的爬虫,它就会关闭反射并关闭用户 y 爬虫
  • 谢谢,但该代码会影响其他用户的爬虫吗?
  • 不,如果您每次创建用户时都创建CrawlController 的新实例。检查我的更新答案。
  • 你的意思是如果我创建了 CrawlerController 的新实例,每个爬虫都会有唯一的会话来关闭它?
  • 使用CrawlController 关闭用户的爬虫,因为它提供了对爬虫的抽象。我猜你不必处理会话 ID
猜你喜欢
  • 2014-08-13
  • 2019-11-07
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2019-06-24
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多