package com.si.demo;
import org.jsoup.nodes.Document;
import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;
public class MyCrawler extends BreadthCrawler {
public MyCrawler(String crawlPath, boolean autoParse) {
super(crawlPath, autoParse);
//添加初始页面
this.addSeed("https://www.taobao.com/");
/*获取像这样的http://news.hfut.edu.cn/show-xxxxxxhtmlurl;设置页面过滤规则,‘+’是可通过的,‘-’是不可通过的*/
this.addRegex("+https://www.taobao.com/market/nvxie/.*");
/*不获取这样的格式 jpg|png|gif*/
this.addRegex("-.*\\.(jpg|png|gif).*");
/*也不要这样的 #*/
this.addRegex("-.*#.*");
}
//自己设置获取信息
public void visit(Page page, CrawlDatums next) {
String url = page.url();
/*如果是一个新的页面*/
if (page.matchUrl("https://www.taobao.com/market/nvxie/.*")) {
/*用JSOUP去解析这个页面*/
Document doc = page.doc();
/*的提取新闻和标题的css选择器*/
String title = page.select("head title").first().text();
String content = page.select("ul li").text();
System.out.println("URL:\n" + url);
System.out.println("title:\n" + title);
System.out.println("content:\n" + content);
/*如果你想要抓取新的url,添加他们到 nextLink*/
/*WebCollector 可以自动去重*/
/*如果autoparse 为真则添加链接去nextlinks 如果与正则不匹配则不会添加*/
//next.add("http://xxxxxx.com");
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
MyCrawler crawler = new MyCrawler("crawl", true);
crawler.setThreads(5);
crawler.setResumable(true);
/*网页爬取深度为4*/
crawler.start(4);
}
}