在循环内调用控制器(crawler4j-3.5)

时间:2015-05-19 10:49:43

标签: java web-crawler crawler4j

您好我在controller内拨打for-loop,因为我有超过100个网址,因此我列出了所有内容,我将迭代并crawl页面,我设置也就是setCustomData的url,因为它不应该离开域。

for (Iterator<String> iterator = ifList.listIterator(); iterator.hasNext();) {
    String str = iterator.next();
    System.out.println("cheking"+str);
    CrawlController controller = new CrawlController(config, pageFetcher,
        robotstxtServer);
    controller.setCustomData(str);
    controller.addSeed(str);
    controller.startNonBlocking(BasicCrawler.class, numberOfCrawlers);
    controller.waitUntilFinish();
}

但如果我运行上面的代码,在第二个网址开始后第一个网址完全抓取并打印错误如下所示。

50982 [main] INFO edu.uci.ics.crawler4j.crawler.CrawlController  - Crawler 1 started.
51982 [Crawler 1] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager  - Connection request: [route: {}->http://www.connectzone.in][total kept alive: 0; route allocated: 0 of 100; total allocated: 0 of 100]
60985 [Thread-2] INFO edu.uci.ics.crawler4j.crawler.CrawlController  - It looks like no thread is working, waiting for 10 seconds to make sure...
70986 [Thread-2] INFO edu.uci.ics.crawler4j.crawler.CrawlController  - No thread is working and no more URLs are in queue waiting for another 10 seconds to make sure...
80986 [Thread-2] INFO edu.uci.ics.crawler4j.crawler.CrawlController  - All of the crawlers are stopped. Finishing the process...
80987 [Thread-2] INFO edu.uci.ics.crawler4j.crawler.CrawlController  - Waiting for 10 seconds before final clean up...
91050 [Thread-2] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager  - Connection manager is shutting down
91051 [Thread-2] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager  - Connection manager shut down

请帮我解决上面的解决方案,我想开始并在内部循环中运行控制器,因为我在列表中有很多url。

注意:**我使用的是** crawler4j-3.5.jar 及其依赖项。

1 个答案:

答案 0 :(得分:0)

尝试:

for(String url : urls) {
    controller.addSeed(url);
}

并覆盖shouldVisit(WebUrl),以便它不会离开域。