Crawler4j快速入门实例

来源:互联网 发布:js判断有无滚动条 编辑:程序博客网 时间:2024/06/16 00:40

crawler4j是Java实现的开源网络爬虫。提供了简单易用的接口,可以在几分钟内创建一个多线程网络爬虫。

github地址:https://github.com/yasserg/crawler4j
我们这里使用maven构建

<dependency>      <groupId>edu.uci.ics</groupId>      <artifactId>crawler4j</artifactId>      <version>4.2</version> </dependency>

编写自定义爬虫类MyCrawler

package com.hbk.test;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;/** * 自定义爬虫类需要继承WebCrawler类,决定哪些url可以被爬以及处理爬取的页面信息 *  * @author 黄宝康 2017年8月29日 下午2:24:06 */public class MyCrawler extends WebCrawler {    /**     * 正则匹配指定的后缀文件     */    private final static Pattern FILTERS = Pattern.compile(".*(\\.(css|js|gif|jpg|png|mp3|mp3|zip|gz))$");    /**     * 这个方法主要是决定哪些url我们需要抓取,返回true表示是我们需要的,返回false表示不是我们需要的Url     * 第一个参数referringPage封装了当前爬取的页面信息 第二个参数url封装了当前爬取的页面url信息     */    @Override    public boolean shouldVisit(Page referringPage, WebURL url) {        String href = url.getURL().toLowerCase(); // 得到小写的url        return !FILTERS.matcher(href).matches() // 正则匹配,过滤掉我们不需要的后缀文件                && href.startsWith("http://zhdw.71zhihui.com"); // url必须是http://zhdw.71zhihui.com开头,规定站点    }    /**     * 当我们爬到我们需要的页面,这个方法会被调用,我们可以尽情的处理这个页面 page参数封装了所有页面信息     */    @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);             System.out.println("html: " + html);             System.out.println("输出链接个数: " + links.size());        }    }}

编写爬虫控制器类Controller

package com.hbk.test;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;/** * 爬虫控制器 * @author 黄宝康 * 2017年8月29日 下午2:36:33 */public class Controller {    public static void main(String[] args) throws Exception {        String crawlStorageFolder = "c:/crawl"; // 定义爬虫数据存储位置        int numberOfCrawlers = 7; // 定义7个爬虫,也就是7个线程        CrawlConfig config = new CrawlConfig(); // 定义爬虫配置        config.setCrawlStorageFolder(crawlStorageFolder); // 设置爬虫文件存储位置        /*         * 实例化爬虫控制器         */        PageFetcher pageFetcher = new PageFetcher(config); // 实例化页面获取器        RobotstxtConfig robotstxtConfig = new RobotstxtConfig(); // 实例化爬虫机器人配置 比如可以设置 user-agent        // 实例化爬虫机器人对目标服务器的配置,每个网站都有一个robots.txt文件 规定了该网站哪些页面可以爬,哪些页面禁止爬,该类是对robots.txt规范的实现        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);         // 实例化爬虫控制器        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);        /**         * 配置爬虫种子页面,就是规定的从哪里开始爬,可以配置多个种子页面         */        controller.addSeed("http://zhdw.71zhihui.com");        /**         * 启动爬虫,爬虫从此刻开始执行爬虫任务,根据以上配置         */        controller.start(MyCrawler.class, numberOfCrawlers);    }}

运行Controller的main方法,爬起页面结果在控制台中输出了html代码数据等。

这里写图片描述

项目中只用到了两个类,一个pom.xml配置jar文件依赖,适合初学者学习Crawler4j入门。

这里写图片描述

并且在磁盘上自动生成了以下文件:
这里写图片描述

原创粉丝点击