内容简介:需要实现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(); } }
运行以后可以看到输出
爬虫监控
获取到单例以后,爬虫注入进入即可
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(); } }
接着启动监控
可以在运行的时候,查看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注解,表示抽取的样式规则.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- TiDB入门(四):从入门到“跑路”
- MyBatis从入门到精通(一):MyBatis入门
- MyBatis从入门到精通(一):MyBatis入门
- Docker入门(一)用hello world入门docker
- 赵童鞋带你入门PHP(六) ThinkPHP框架入门
- 初学者入门 Golang 的学习型项目,go入门项目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Ext JS源码分析与开发实例宝典
彭仁夔 / 电子工业出版社 / 2010-1 / 78.00元
《Ext JS源码分析与开发实例宝典》从Ext JS实现的基本功能开始讲解,从两个方面对Ext JS进行整体上的概述,让读者从宏观上去把握ExtJS框架。接下来讲解Ext JS核心基础知识,包括事件机制、模板模型、数据模型以及对类、函数、字符串、日期、数组及定时任务这6个类进行扩展。然后讲解Ext JS基于元素的开发,包括动画特效和拖曳实现等。最后深入讲解组件的开发,对布局、模型及4大组件一一进行......一起来看看 《Ext JS源码分析与开发实例宝典》 这本书的介绍吧!