WebMagic 入门

栏目: 编程工具 · 发布时间: 6年前

内容简介:需要实现PageProcessor接口编写main函数

引入包

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();
    }
}

运行以后可以看到输出

WebMagic 入门

爬虫监控

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

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();

    }
}

接着启动监控

WebMagic 入门

WebMagic 入门

WebMagic 入门

WebMagic 入门

可以在运行的时候,查看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注解,表示抽取的样式规则.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

产品经理的20堂必修课

产品经理的20堂必修课

徐建极 / 人民邮电出版社 / 2013-9-1 / 59.00元

《产品经理的20堂必修课》以作者八年的产品经理工作实践为基础,通过系统的理论结合丰富的实例的方法,全面地总结了作为一名互联网产品经理所应掌握的知识。 《产品经理的20堂必修课》分为三大部分。 讲产品:深入剖析互联网产品成功的要素,分别从需求导向、简单原则、产品运营、战略布局等维度,分析如何让产品在残酷的互联网竞争中脱颖而出。 讲方法:着重分析优秀的产品团队运作的工作方法和程序,详......一起来看看 《产品经理的20堂必修课》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具