创建一个Maven项目
在pom.xml中加入
<dependency>
<groupId>edu.uci.ics</groupId>
<artifactId>crawler4j</artifactId>
<version>4.3</version>
</dependency>
项目代码组织:
Controller类:
package com.yj.WebCrawlTest;
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 {
String crawlStorageFolder = "E:/crawler";// 定义爬虫数据存储位置
int numberOfCrawlers = 7;// 定义了7个爬虫,也就是7个线程
CrawlConfig config = new CrawlConfig();// 定义爬虫配置
config.setCrawlStorageFolder(crawlStorageFolder);// 设置爬虫文件存储位置
config.setMaxDepthOfCrawling(1);// 设置爬虫深度为1,种子本身深度为0
// crawlConfig.setMaxPagesToFetch(maxPagesToFetch);//设置最多爬多少个页面
// config.setIncludeHttpsPages(true);//启用SSL
// crawlConfig.setIncludeBinaryContentInCrawling(true);//启用二进制爬取
// crawlConfig.setPolitenessDelay(politenessDelay);// 设置等待时间,默认大于200ms
/*
* 实例化爬虫控制器。
*/
PageFetcher pageFetcher = new PageFetcher(config);// 实例化页面获取器
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();// 实例化爬虫机器人配置
// 实例化爬虫机器人对目标服务器的配置,每个网站都有一个robots.txt文件
// 规定了该网站哪些页面可以爬,哪些页面禁止爬,该类是对robots.txt规范的实现
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig,
pageFetcher);
// 实例化爬虫控制器
CrawlController controller = new CrawlController(config, pageFetcher,
robotstxtServer);
/*
* 对于每次抓取,您需要添加一些种子网址。 这些是抓取的第一个URL,然后抓取工具开始跟随这些页面中的链接
*/
controller.addSeed("https://blog.youkuaiyun.com/LY_Dengle/");
// controller.addSeed("http://www.baidu.com/");
// controller.addSeed("http://www.ics.uci.edu/");
/**
* 启动爬虫,爬虫从此刻开始执行爬虫任务,根据以上配置
*/
controller.start(MyCrawler.class, numberOfCrawlers);
}
}
MyCrawler类:
/**
* YangJie
* 2018年11月21日下午9:09:15
*/
package com.yj.WebCrawlTest;
import java.util.Set;
import java.util.regex.Pattern;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;
/**
* <p>
* MyCrawler
* </p>
* <p>
* Description:
* </p>
*
* @author YangJie
* @data 2018年11月21日下午9:09:15
* @version 1.0
*/
public class MyCrawler extends WebCrawler {
/**
* 正则表达式匹配指定的后缀文件
*/
private final static Pattern FILTERS = Pattern
.compile(".*(\\.(css|js|gif|jpg" + "|png|mp3|mp4|zip|gz))$");
/**
* 这个方法主要是决定哪些url我们需要抓取,返回true表示是我们需要的,返回false表示不是我们需要的Url
* 第一个参数referringPage封装了当前爬取的页面信息 第二个参数url封装了当前爬取的页面url信息
* 在这个例子中,我们指定爬虫忽略具有css,js,git,...扩展名的url,只接受以“http://www.ics.uci.edu/”开头的url。
* 在这种情况下,我们不需要referringPage参数来做出决定。
*/
@Override
public boolean shouldVisit(Page referringPage, WebURL url) {
String href = url.getURL().toLowerCase();// 得到小写的url
return !FILTERS.matcher(href).matches() // 正则匹配,过滤掉我们不需要的后缀文件
&& href.startsWith("https://blog.youkuaiyun.com/");// 只接受以“http://www.ics.uci.edu/”开头的url
}
/**
* 当一个页面被提取并准备好被你的程序处理时,这个函数被调用。
*/
@Override
public void visit(Page page) {
String url = page.getWebURL().getURL();// 获取url
System.out.println("URL: " + url);
if (page.getParseData() instanceof HtmlParseData) {// 判断是否是html数据
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();//// 强制类型转换,获取html数据对象
String text = htmlParseData.getText();// 获取页面纯文本(无html标签)
String html = htmlParseData.getHtml();// 获取页面Html
Set<WebURL> links = htmlParseData.getOutgoingUrls();// 获取页面输出链接
System.out.println("纯文本长度: " + text.length());
System.out.println("html长度: " + html.length());
System.out.println("链接个数 " + links.size());
}
}
}
运行结果部分截图:
源代码下载
GitHub地址:https://github.com/1996jie/WebCrawlTest
优快云下载:https://download.youkuaiyun.com/download/baidu_27989705/10813401