WebCollector教程——获取当前深度
来源:互联网 发布:零基础学java 编辑:程序博客网 时间:2024/05/27 01:17
本教程演示了WebCollector 2.20的新特性。
下载
WebCollector最新jar包可在WebCollector github主页下载。
内容:
本教程和深度遍历没有任何关系,只是讲解如何获取当前网页在遍历树中的深度。
利用2.20版本中的新特性MetaData可以轻松实现这个功能。
import cn.edu.hfut.dmic.webcollector.model.CrawlDatum;import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;import cn.edu.hfut.dmic.webcollector.model.Page;import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;/** * 本教程和深度遍历没有任何关系 * 一些爬取需求希望加入深度信息,即遍历树中网页的层 * 利用2.20版本中的新特性MetaData可以轻松实现这个功能 * * @author hu */public class DemoDepthCrawler extends BreadthCrawler{ public DemoDepthCrawler(String crawlPath, boolean autoParse) { super(crawlPath, autoParse); } @Override public void visit(Page page, CrawlDatums next) { System.out.println("visiting:"+page.getUrl()+"\tdepth="+page.meta("depth")); } @Override protected void afterParse(Page page, CrawlDatums next) { //当前页面的depth为x,则从当前页面解析的后续任务的depth为x+1 int depth; //如果在添加种子时忘记添加depth信息,可以通过这种方式保证程序不出错 if(page.meta("depth")==null){ depth=1; }else{ depth=Integer.valueOf(page.meta("depth")); } depth++; for(CrawlDatum datum:next){ datum.meta("depth", depth+""); } } public static void main(String[] args) throws Exception { DemoDepthCrawler crawler=new DemoDepthCrawler("depth_crawler", true); for(int i=1;i<=5;i++){ crawler.addSeed(new CrawlDatum("http://news.hfut.edu.cn/list-1-"+i+".html") .meta("depth", "1")); } /*正则规则用于控制爬虫自动解析出的链接,用户手动添加的链接,例如添加的种子、或 在visit方法中添加到next中的链接并不会参与正则过滤*/ /*自动爬取类似"http://news.hfut.edu.cn/show-xxxxxxhtml"的链接*/ crawler.addRegex("http://news.hfut.edu.cn/show-.*html"); /*不要爬取jpg|png|gif*/ crawler.addRegex("-.*\\.(jpg|png|gif).*"); /*不要爬取包含"#"的链接*/ crawler.addRegex("-.*#.*"); crawler.setTopN(5); crawler.start(3); }}
通过捐款支持WebCollector
维护WebCollector及教程需要花费较大的时间和精力,如果你喜欢WebCollector的话,欢迎通过捐款的方式,支持开发者的工作,非常感谢!
你可以使用支付宝钱包扫描下方的二维码进行捐款, 或者通过向支付宝帐号 hujunxianligong@126.com转帐进行捐款。
0 0
- WebCollector教程——获取当前深度
- WebCollector教程——爬取搜索引擎
- JAVA网络爬虫WebCollector深度解析——爬虫内核
- JAVA网络爬虫WebCollector深度解析——爬虫内核
- WebCollector教程——在Eclipse项目中配置使用WebCollector爬虫
- WebCollector教程——爬取新浪微博
- WebCollector ——MetaData
- WebCollector初学教程
- WebCollector 2.x入门教程——基本概念
- WebCollector——断点爬取
- JAVA爬虫WebCollector教程列表
- WebCollector 2.x 教程列表
- JAVA爬虫WebCollector 2.x入门教程——基本概念
- JAVA爬虫WebCollector 2.x入门教程——基本概念
- WebCollector内核开发——定制Http请求
- WebCollector初探——微博信息爬取
- WebCollector内核解析—如何设计一个爬虫
- WebCollector内核解析—如何设计一个爬虫
- iOS SHA1算法
- HTML5+CSS3前端开发资源整合
- WebCollector教程——爬取新浪微博
- WebCollector教程——爬取搜索引擎
- Rodeo使用体验
- WebCollector教程——获取当前深度
- KK's Steel bestcoder round 71 hdu 5620(裴波那契)
- JAVA爬虫WebCollector 2.x入门教程——基本概念
- WebCollector设置代理
- WebCollector网页正文提取
- CPU指令结构
- Android数据库操作
- BPMN 2.0规范详解
- 机器学习十大算法