Introduction
WebCollector is an open source web crawler framework based on Java. It provides some simple interfaces for crawling the Web, you can set up a multi-threaded web crawler in less than 5 minutes.
Installation
WebCollector jars are available on the HomePage.
- webcollector-version-bin.zip contains core jars
- webcollector_example-version-bin.zip contains core jars with examples
Demo
Let's crawl some news from Yahoo.This demo prints out the titles and contents extracted from news of Yahoo.
YahooCrawler.java:
import cn.edu.hfut.dmic.webcollector.crawler.BreadthCrawler;
import cn.edu.hfut.dmic.webcollector.model.Links;
import cn.edu.hfut.dmic.webcollector.model.Page;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
public class YahooCrawler extends BreadthCrawler {
public YahooCrawler(String crawlPath, boolean autoParse) {
super(crawlPath, autoParse);
this.addSeed("http://news.yahoo.com/");
this.addRegex("http://news.yahoo.com/.*");
this.addRegex("-http://news.yahoo.com/.+/.*");
this.addRegex("-.*\\.(jpg|png|gif).*");
this.addRegex("-.*#.*");
}
@Override
public void visit(Page page, Links nextLinks) {
String url = page.getUrl();
if (Pattern.matches("http://news.yahoo.com/.+html", url)) {
Document doc = page.getDoc();
String title = doc.select("h1[class=headline]").first().text();
String content = doc.select("div[class=body yom-art-content clearfix]").first().text();
System.out.println("URL:\n" + url);
System.out.println("title:\n" + title);
System.out.println("content:\n" + content);
}
}
public static void main(String[] args) throws Exception {
YahooCrawler crawler = new YahooCrawler("crawl", true);
crawler.setThreads(50);
crawler.setTopN(100);
crawler.start(4);
}
}