java爬虫 webcollector_java爬虫爬取Elastic中文社区用作es测试数据

前言

为了测试es的完美功能,笔者使用爬虫爬取了Elastic中文社区和优快云的大量数据,作为测试之用,下面简单介绍一下折腾的过程

认识 WebCollector

WebCollector是一个无须配置、便于二次开发的JAVA爬虫框架(内核),它提供精简的的API,只需少量代码即可实现一个功能强大的爬虫。WebCollector-Hadoop是WebCollector的Hadoop版本,支持分布式爬取。

WebCollector致力于维护一个稳定、可扩的爬虫内核,便于开发者进行灵活的二次开发。内核具有很强的扩展性,用户可以在内核基础上开发自己想要的爬虫。源码中集成了Jsoup,可进行精准的网页解析。2.x版本中集成了selenium,可以处理javascript生成的数据。

官网地址:http://crawlscript.github.io/WebCollector/

使用步骤

导入jar依赖,笔者是maven项目,所有加入如下pom.xml依赖

ps:笔者这里是使用的最新版的,maven仓库目前最新版的是2.09,所以使用最新的就自己下载打包吧

环境有了后,直接新建一个类继承BreadthCrawler类重新​visit方法,你的处理逻辑都在visit方法里面,下面楼主贴下我的代码

​爬取Elastic中文社区资源

/**

* Created by 小陈 on 2016/3/29.

*/

@Component

public class ElasticCrawler extends BreadthCrawler {

@Autowired

IpaDao ipaDao;

public ElasticCrawler() {

super("crawl", true);

/*start page*/

this.addSeed("xxx");

/*fetch url like http://news.hfut.edu.cn/show-xxxxxxhtml*/

this.addRegex("xxx");

/*do not fetch jpg|png|gif*/

this.addRegex("-.*\\.(jpg|png|gif).*");

/*do not fetch url contains #*/

// this.addRegex("-.*#.*");

}

@Override

public void visit(Page page, CrawlDatums next) {

String url = page.getUrl();

String content="";

try {

content = ContentExtractor.getContentByUrl(url);

}catch (Exception e){

e.printStackTrace();

}

/*抽取标题*/

String title=page.getDoc().title();

System.out.println("-------------------->"+title);

if(!title.isEmpty() && ! content.isEmpty()){

Pa pa=new Pa(title,content);

ipaDao.save(pa);//持久化到数据库

}

}爬取优快云资源

/**

* @author kl by 2016/3/29

* @boke www.kailing.pub

*/

@Component

public class 优快云Crawler extends BreadthCrawler {

@Autowired

IpaDao ipaDao;

public 优快云Crawler() {

super("crawl", true);

/*start page*/

this.addSeed("http://blog.youkuaiyun.com/.*");//添加种子地址

/*fetch url like http://news.hfut.edu.cn/show-xxxxxxhtml*/

this.addRegex("http://blog.youkuaiyun.com/.*/article/details/.*");

/*do not fetch jpg|png|gif*/

this.addRegex("-.*\\.(jpg|png|gif).*");

/*do not fetch url contains #*/

// this.addRegex("-.*#.*");

}

@Override

public void visit(Page page, CrawlDatums next) {

String url = page.getUrl();

String content="";

try {

content = ContentExtractor.getContentByUrl(url);

}catch (Exception e){

e.printStackTrace();

}

if (page.matchUrl("http://blog.youkuaiyun.com/.*/article/details/.*")) {

String title = page.select("div[class=article_title]").first().text();

String author = page.select("div[id=blog_userface]").first().text();//获取作者名

System.out.println("title:" + title + "\tauthor:" + author);

if(!title.isEmpty() && ! content.isEmpty()){

Pa pa=new Pa(title,content);

ipaDao.save(pa);

}

}

}ps:Elastic中文社区的爬取规则和谐了,楼主是爱社区的,大家可以放心的爬优快云吧,WebCollector功能很强大,爬虫的一个关键就是需要知道网站的url规则,有兴趣的可以研究​ 下,Elastic的数据不多,分吧钟就够了,优快云爬了5,6分钟,没有做深度的爬,取了大概二三十万的数据样子,只取标题和正文

973bdb8f3e989a01b70c9d912a3e3f98.png

4c34c39dd8f5e07e3e5409d7feee104b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值