使用webmagic搭建一个简单的爬虫

WebMagic是一个简单灵活的Java爬虫框架。基于WebMagic,你可以快速开发出一个高效、易维护的爬虫。

使用Gradle构建,只需要引入两个依赖

compile group: 'us.codecraft', name: 'webmagic-core', version: '0.7.3'
    compile group: 'us.codecraft', name: 'webmagic-extension', version: '0.7.3'

我们想要爬取CSDN某个用户的所有博文标题以及连接。
http://blog.csdn.net/lgh1992314/article/list/1 页面我们可以得到该用户的博文总页数:
使用webmagic搭建一个简单的爬虫_第1张图片
每一页都有如下的博文列表:
使用webmagic搭建一个简单的爬虫_第2张图片
我们想要获取的内容就在如下标签下:
使用webmagic搭建一个简单的爬虫_第3张图片

详细代码:

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

public class GithubRepoPageProcessor implements PageProcessor {

    //CSDN用户名
    private static final String username = "yerenyuan_pku";
    private  static int count = 0;
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

    @Override
    public void process(Page page) {
        //判断链接是否符合 http://blog.csdn.net/用户名/article/list/任意个数字
        if (!page.getUrl().regex("http://blog.csdn.net/" + username + "/article/list/[0-9]+").match()) {
            String pageLists = page.getHtml().xpath("//*[@id=\"papelist\"]/span").get();
            int startPos = pageLists.indexOf("共");
            int endPos = pageLists.indexOf("页");

            //获得CSDN博客的总页数
            int pages = Integer.parseInt(pageLists.substring(startPos + 1, endPos));
//            System.out.println("一共 " + pages + " 页");

            //加入满足条件的链接
            for (int i = 1; i <= pages; i++) {
                page.addTargetRequest("http://blog.csdn.net/"+username + "/article/list/" + i);
            }
        } else {
            List nodes = page.getHtml().xpath("//*[@id=\"article_list\"]/div[@class=\"list_item article_item\"]").nodes();
            System.out.println(nodes.size());
            for (Selectable s : nodes) {
                //获取页面需要的内容
                System.out.println(s.xpath("//*[@class=\"article_title\"]/h1/span/a/text()") + "  " +
                        "http://blog.csdn.net" + s.xpath("//*[@class=\"article_title\"]/h1/span/a/@href"));
                count++;
            }
        }


    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        long startTime, endTime;
        System.out.println("开始爬取...");
        startTime = System.currentTimeMillis();
        Spider.create(new GithubRepoPageProcessor()).addUrl("http://blog.csdn.net/" + username).thread(5).run();
        endTime = System.currentTimeMillis();
        System.out.println("爬取结束,耗时约" + ((endTime - startTime) / 1000) + "秒,抓取了"+ count +"条记录");
    }
}

结果:

...
 8086/8088指令系统   http://blog.csdn.net/x_iya/article/details/8136924
 ITAT复赛答案集锦(1)   http://blog.csdn.net/x_iya/article/details/8134400
 C++面试宝典2011版   http://blog.csdn.net/x_iya/article/details/8055391
get page: http://blog.csdn.net/x_iya/article/list/51
爬取结束,耗时约15秒,抓取了1004条记录

你可能感兴趣的:(使用webmagic搭建一个简单的爬虫)