我正在尝试使用crawler4j,因为它显示在this示例中使用,无论我如何定义抓取工具的数量或更改根文件夹我继续从代码中获取此错误:< / p>
“需要的参数: rootFolder(它将包含中间爬网数据) numberOfCralwers(并发线程数)“ 主要代码如下:
public class Controller {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Needed parameters: ");
System.out.println("\t rootFolder (it will contain intermediate crawl data)");
System.out.println("\t numberOfCralwers (number of concurrent threads)");
return;
}
/*
* crawlStorageFolder is a folder where intermediate crawl data is
* stored.
*/
String crawlStorageFolder = args[0];
/*
* numberOfCrawlers shows the number of concurrent threads that should
* be initiated for crawling.
*/
int numberOfCrawlers = Integer.parseInt(args[1]);
有一个类似的问题确切地问我想要知道here,但我不太明白解决方案,比如我要输入 java BasicCrawler Controller“arg1”“arg2”< / strong>。我在Eclipse上运行此代码,我仍然是编程领域的新手。如果有人帮助我理解这个问题我真的很感激
答案 0 :(得分:0)
要在项目中使用crawler4j,您必须创建两个类。其中一个是CrawlController(根据参数启动爬虫),另一个是Crawler。
只需在Controller类中运行main方法,然后查看已爬网的页面
这是Controller.java文件:
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
public class Controller {
public static void main(String[] args) throws Exception {
RobotstxtConfig robotstxtConfig2 = new RobotstxtConfig();
System.out.println(robotstxtConfig2.getCacheSize());
System.out.println(robotstxtConfig2.getUserAgentName());
String crawlStorageFolder = "/crawler/testdata";
int numberOfCrawlers = 4;
CrawlConfig config = new CrawlConfig();
config.setCrawlStorageFolder(crawlStorageFolder);
PageFetcher pageFetcher = new PageFetcher(config);
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
System.out.println(robotstxtConfig.getCacheSize());
System.out.println(robotstxtConfig.getUserAgentName());
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
CrawlController controller = new CrawlController(config,
pageFetcher, robotstxtServer);
controller.addSeed("http://cyesilkaya.wordpress.com/");
controller.start(Crawler.class, numberOfCrawlers);
}
}
这是Crawler.java文件:
import java.io.IOException;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.url.WebURL;
public class Crawler extends WebCrawler {
@Override
public boolean shouldVisit(WebURL url) {
// you can write your own filter to decide crawl the incoming URL or not.
return true;
}
@Override
public void visit(Page page) {
String url = page.getWebURL().getURL();
try {
String url = page.getWebURL().getURL();
System.out.println("URL: " + url);
}
catch (IOException e) {
}
}
}
答案 1 :(得分:0)
如果在运行文件时没有给出任何参数,则会出现该错误。 将以下内容作为注释使用您的代码或删除它。
if (args.length != 2) {
System.out.println("Needed parameters: ");
System.out.println("\t rootFolder (it will contain intermediate crawl data)");
System.out.println("\t numberOfCralwers (number of concurrent threads)");
return;
}
然后将根文件夹设置为要存储元数据的文件夹。
答案 2 :(得分:0)
在Eclipse中: - &gt;点击运行 - &gt;点击运行配置...
在弹出窗口中:
首先,左栏:确保在子目录Java应用程序中选择了您的应用程序,否则创建一个新的(单击新的)。
然后在中央窗口中,继续&#34;参数&#34;
在&#34;程序参数&#34;下编写您的参数;一旦你写了你的第一个参数,按回车键进入seconde参数,依此类推...(= newline因为args是[])
然后单击“应用”
然后点击“运行”。