引入包

https://search.maven.org/artifact/us.codecraft/webmagic-core/0.7.3/jar

    <dependencies>
        <dependency>
            <groupId>us.codecraft</groupId>
            <artifactId>webmagic-extension</artifactId>
            <version>0.7.3</version>
        </dependency>
        <dependency>
            <groupId>us.codecraft</groupId>
            <artifactId>webmagic-core</artifactId>
            <version>0.7.3</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

编写最基本的爬虫

需要实现PageProcessor接口

package reptile;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * @author ming
 *
 */
public class RepoPageProcessor implements PageProcessor {
    /*
     爬虫相关配置
     设置抓取间隔 重试次数
    */
    private Site site = Site.me().setRetryTimes(10).setTimeOut(100000);
    /**
     * process the page, extract urls to fetch, extract the data and store
     * 此处是爬虫的核心逻辑
     * @param page page
     */
    public void process(Page page) {
        // 获取到页面html
        page.putField("title", page.getHtml().$("div.content").toString());

        // 添加url到队列中
        List<String> urls = page.getHtml().$("h3").links().all();
        // 从页面后续,继续读取url来获取
        page.addTargetRequests(urls);
    }

    /**
     * get the site settings
     *
     * @return site
     * @see Site
     */
    public Site getSite() {
        return this.site;
    }
}

编写main函数

import reptile.RepoPageProcessor;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;

public class main {
    public static void main(String[] args){
        Spider.create(new RepoPageProcessor())
                // 确定开始爬的地址
                .addUrl("https://www.iming.info/")
                // 保存为json
                .addPipeline(new JsonFilePipeline("./result"))
                // 开启五个线程爬取
                .thread(100)
                // 启动爬虫
                .run();
    }
}

运行以后可以看到输出
2019-04-04-22-11-52----

爬虫监控

获取到单例以后,爬虫注入进入即可

import reptile.RepoPageProcessor;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;

import javax.management.JMException;

public class main {
    public static void main(String[] args){
        Spider spider = Spider.create(new RepoPageProcessor())
                // 确定开始爬的地址
                .addUrl("https://www.iming.info/")
                // 保存为json
                .addPipeline(new JsonFilePipeline("./result"));

        try {
            SpiderMonitor.instance().register(spider);
        } catch (JMException e) {
            e.printStackTrace();
        }

        spider.start();

    }
}

接着启动监控
2019-04-04-22-45-42----

2019-04-04-22-46-29----

2019-04-04-22-48-34----

2019-04-04-22-52-46----

可以在运行的时候,查看jvm监控

注解爬虫

先编写model层

package Model;

public class iming {
    private String title;

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

然后,所有的类,规定从那个页面中获取

package Model;

import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

// 最终要抓取的url 最终想要的数据在这里,使用正则匹配需要的url
@TargetUrl("https://github.com/\\w+/\\w+")
// 需要从那个页面中获取到url
@HelpUrl("https://github.com/\\w+")
public class GithubRepo {
    private String name;

    private String author;

    private String readme;

}

然后,再次使用ExtractBy注解,表示抽取的样式规则.