0 环境准备

创建 maven 工程,加入依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.ys</groupId><artifactId>crawler-webmagic</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- webmagic核心包 --><dependency><groupId>us.codecraft</groupId><artifactId>webmagic-core</artifactId><version>0.7.3</version></dependency><!-- webmagic扩展包 --><dependency><groupId>us.codecraft</groupId><artifactId>webmagic-extension</artifactId><version>0.7.3</version></dependency></dependencies></project>

注意:0.7.3 版本对 SSL 的并不完全,如果是直接从 Maven 中央仓库下载依赖,在爬取只支持 SSL v1.2 的网站时会有 SSL 的异常抛出。
解决方案:1、等作者的 0.7.4 的版本发布;2、直接从 github 上下载最新代码,安装到本地仓库;3、也可以参考以下资料自己修复:https://github.com/code4craft/webmagic/issues/701

加入日志配置文件

WebMagic 使用 slf4j-log4j12 作为 slf4j 的实现。

添加 log4j.properties

log4j.rootLogger = DEBUG,A1log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

1 测试

需求:编写爬虫程序,爬取 CSDN 中博客 —— 人工智能 的内容。
网址:https://blog.csdn.net/nav/ai

package cn.ys.webmagic.test;import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;public class JobProcessor implements PageProcessor {//解析页面public void process(Page page) {//解析返回的数据page,并且把解析的结果放到ResultItems中page.putField("div", page.getHtml().css("div.unin_reason_dialog h3").all());}private Site site = Site.me();public Site getSite() {return site;}//主函数,执行爬虫public static void main(String[] args) {Spider.create(new JobProcessor()).addUrl("https://blog.csdn.net/nav/ai")  //设置爬取数据的页面.run();                                  //执行爬虫}
}

控制台打印:

get page: https://blog.csdn.net/nav/ai
div:    [<h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>]
2019-07-31 15:55:18,138 [main] [us.codecraft.webmagic.Spider]-[INFO] Spider blog.csdn.net closed! 1 pages downloaded.

注意:Spider 是爬虫启动的入口。在启动爬虫之前,我们需要使用一个 PageProcessor 创建一个 Spider 对象,然后使用 run() 进行启动。

2 实现 PageProcessor

2.1 抽取元素 Selectable

WebMagic 里面主要使用类三种抽取技术:XPath、正则表达式和CSS选择器。另外,对于 JSON 格式的内容,可使用 JsonPath 进行解析。

1⃣️ XPath

XPath,即为XML路径语言(XMLPathLanguage),它是一种用来确定XML文档中某部分位置的语言。XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似。

以上是获取属性 class=mt 的 div 标签,里面的 h1 标签的内容。

page.putField("div2",page.getHtml().xpath("//div[@class=title]/h2/a"));

2⃣️ CSS 选择器

CSS 选择器是与 XPath 类似的语言。它比 XPath 写起来要简单一些,但是如果写复杂一点的抽取规则,就相对麻烦一些。

div.mt > h1:表示 class 为 mt 的 div 标签下的直接子元素 h1 标签。

3⃣️ 正则表达式

package cn.ys.webmagic.test;import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;public class JobProcessor implements PageProcessor {//解析页面public void process(Page page) {//解析返回的数据page,并且把解析的结果放到ResultItems中page.putField("div", page.getHtml().css("div.unin_reason_dialog h3").all());//XPathpage.putField("div2",page.getHtml().xpath("//div[@class=title]/h2/a"));//正则表达式page.putField("div3",page.getHtml().css("div.title h2 a").regex(".*数学.*").all());}private Site site = Site.me();public Site getSite() {return site;}//主函数,执行爬虫public static void main(String[] args) {Spider.create(new JobProcessor()).addUrl("https://blog.csdn.net/nav/ai")  //设置爬取数据的页面.run();                                  //执行爬虫}
}

控制台打印:

get page: https://blog.csdn.net/nav/ai
div:    [<h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>, <h3>选择理由,精准屏蔽</h3>]
div2:   <a href="https://blog.csdn.net/qq_40798435/article/details/95489469" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_40798435/article/details/95489469&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;0&quot;}"> 建筑施工借助物联网卡保障高楼安全 </a>
div3:   [<a href="https://blog.csdn.net/qq_40798435/article/details/95489469" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_40798435/article/details/95489469&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;0&quot;}"> 建筑施工借助物联网卡保障高楼安全 </a>, <a href="https://blog.csdn.net/yyykj/article/details/95322311" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/yyykj/article/details/95322311&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;1&quot;}"> 转战物联网·基础篇02-物联网中的角儿 </a>, <a href="https://blog.csdn.net/weixin_43532158/article/details/95323836" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_43532158/article/details/95323836&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;2&quot;}"> 基于物联网的智能家居 </a>, <a href="https://blog.csdn.net/weixin_44452342/article/details/95049283" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_44452342/article/details/95049283&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;3&quot;}"> ZT交易所即将上线BRC(贝尔链) </a>, <a href="https://blog.csdn.net/ss5214423/article/details/96136274" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/ss5214423/article/details/96136274&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;4&quot;}"> Unity3D游戏开发笔记-2【优化】 </a>, <a href="https://blog.csdn.net/qq_40798435/article/details/95628250" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_40798435/article/details/95628250&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;5&quot;}"> 医疗行业需要物联网卡能做什么? </a>, <a href="https://blog.csdn.net/weixin_43830887/article/details/96835533" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_43830887/article/details/96835533&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;6&quot;}"> 在新加坡成立基金会做ICO交易所合规白皮书你应该知道的几个要点 </a>, <a href="https://blog.csdn.net/qq_36075612/article/details/97110898" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_36075612/article/details/97110898&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;7&quot;}"> 关注物联网、关注NB-IoT </a>, <a href="https://blog.csdn.net/bemfa/article/details/96564875" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/bemfa/article/details/96564875&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;8&quot;}"> MQTT物联网通信协议概论 </a>, <a href="https://blog.csdn.net/COMC1DU/article/details/94866252" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/COMC1DU/article/details/94866252&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;9&quot;}"> 社链生态全球发布会圆满成功,共建社链梦! </a>, <a href="https://blog.csdn.net/qq_34229678/article/details/96156404" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_34229678/article/details/96156404&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;10&quot;}"> Unity3D Network如何限制玩家人数 </a>, <a href="https://blog.csdn.net/qq_42992919/article/details/95343296" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_42992919/article/details/95343296&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;11&quot;}"> Python图形用户界面和游戏开发 </a>, <a href="https://blog.csdn.net/weixin_42411153/article/details/94840046" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_42411153/article/details/94840046&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;12&quot;}"> 物联网协议--MQTT整理 </a>, <a href="https://blog.csdn.net/kaihuiguoji/article/details/90671898" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/kaihuiguoji/article/details/90671898&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;13&quot;}"> 区块链对区块链金融有什么作用? </a>, <a href="https://blog.csdn.net/weixin_43272781/article/details/94891083" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_43272781/article/details/94891083&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;14&quot;}"> 物联网的发展与应用 </a>, <a href="https://blog.csdn.net/weixin_44452342/article/details/96572844" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_44452342/article/details/96572844&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;15&quot;}"> ZT交易所OTC商家火热招募中 </a>, <a href="https://blog.csdn.net/qq_34229678/article/details/96970866" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_34229678/article/details/96970866&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;16&quot;}"> Unity3D (新)SteamVR 2.0手柄输入与震动 </a>, <a href="https://blog.csdn.net/qq_31243065/article/details/97119523" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_31243065/article/details/97119523&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;17&quot;}"> Win32 游戏开发:贪吃蛇 上篇 </a>, <a href="https://blog.csdn.net/LEON1741/article/details/96888960" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/LEON1741/article/details/96888960&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;18&quot;}"> 【三分钟讲清区块链/比特币】之一:区块链入门教程 </a>, <a href="https://blog.csdn.net/PyhtonChen/article/details/96149214" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/PyhtonChen/article/details/96149214&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;19&quot;}"> Python图形界面游戏开发 </a>, <a href="https://blog.csdn.net/weixin_36628778/article/details/96507085" target="_blank" data-report-click="{&quot;mod&quot;:&quot;popu_459&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/weixin_36628778/article/details/96507085&quot;,&quot;strategy&quot;:&quot;none&quot;,&quot;index&quot;:&quot;20&quot;}"> day-012--图形用户界面和游戏开发 </a>]
2019-07-31 16:02:53,547 [main] [us.codecraft.webmagic.Spider]-[INFO] Spider blog.csdn.net closed! 1 pages downloaded.

2.2 抽取元素 API

Selectable 相关的抽取元素链式 API 是 WebMagic 的一个核心功能。使用 Selectable 接口,可以直接完成页面元素的链式抽取,也无需关心抽取的细节。

上例中可以看到,page.getHtml() 返回的是一个 html 对象,它实现类 Selectable 接口。这个接口包含的方法分为两类:抽取部分获取结果部分

方法 说明 示例
xpath(String xpath) 使用 XPath 选择 html.xpath("//div[@class='title']")
$(String selector) 使用 css 选择器选择 html.$("div.title")
$(String selector,String attr) 使用 css 选择器选择 html.$("div.title","text")
css(String selector) 功能同 $() ,使用 css 选择器选择 html.css("div.title")
links() 选择所有链接 html.links()
regex(String regex) 使用正则表达式抽取 html.regex("\(.\*?)\")

这部分抽取 API 返回的都是一个 Selector 接口,意思是说,是支持链式调用的。

2.3 处理结果 API

下面的方法是选择抽取一个或多个元素:

方法 说明 示例
get() 返回一条 String 类型的结果 String link = html.links().get()
toString() get() ,返回一条 String 类型的结果 String link = html.links().toString()
all() 返回所有抽取的结果 List links = html.links().all()

说明:当有多条数据时,使用 get() 或 toString() 都是获取第一个 url 地址。

2.4 获取链接

1⃣️ 添加目标地址,目标地址正则匹配

我们可以通过添加目标地址,从种子页面爬取到更多的页面

package cn.ys.webmagic.test;import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;public class JobProcessor implements PageProcessor {//解析页面public void process(Page page) {//解析返回的数据page,并且把解析的结果放到ResultItems中page.putField("div", page.getHtml().css("div.unin_reason_dialog h3").all());//XPathpage.putField("div2",page.getHtml().xpath("//div[@class=title]/h2/a"));//正则表达式page.putField("div3",page.getHtml().css("div.title h2 a").regex(".*数学.*").all());//获取链接page.addTargetRequests(page.getHtml().css("div.title").links().regex(".*9$").all());//以9结尾的page.putField("url",page.getHtml().css("div.article-title-box h1"));}private Site site = Site.me();public Site getSite() {return site;}//主函数,执行爬虫public static void main(String[] args) {Spider.create(new JobProcessor()).addUrl("https://blog.csdn.net/nav/ai")  //设置爬取数据的页面.run();                                  //执行爬虫}
}

3 Pipeline

  • ConsolePipeline:控制台输出
  • FilePipeline:文件保存
  • JsonFilePipeline:以 JSON 方式保存
package cn.ys.webmagic.test;import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;public class JobProcessor implements PageProcessor {//解析页面public void process(Page page) {//解析返回的数据page,并且把解析的结果放到ResultItems中page.putField("div", page.getHtml().css("div.unin_reason_dialog h3").all());//XPathpage.putField("div2",page.getHtml().xpath("//div[@class=title]/h2/a"));//正则表达式page.putField("div3",page.getHtml().css("div.title h2 a").regex(".*数学.*").all());//获取链接page.addTargetRequests(page.getHtml().css("div.title").links().regex(".*9$").all());//以9结尾的page.putField("url",page.getHtml().css("div.article-title-box h1"));}private Site site = Site.me();public Site getSite() {return site;}//主函数,执行爬虫public static void main(String[] args) {Spider.create(new JobProcessor()).addUrl("https://blog.csdn.net/nav/ai")  //设置爬取数据的页面
//                .addPipeline(new ConsolePipeline())
//                .addPipeline(new FilePipeline("/Users/yangshuo/jd/")) //设置结果输出到文件.addPipeline(new JsonFilePipeline("/Users/yangshuo/jd/")) //以 JSON 方式保存.thread(5)                               //设置5个线程执行.run();                                  //执行爬虫}
}

3.1 定制 Pipeline

如果以上 Pipeline 都不能满足你的需要,你可以定制 Pipeline

1⃣️ 创建类MyPipeline实现接口Pipeline

package cn.ys.demo;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;public class MyPipeline implements Pipeline {public void process(ResultItems resultItems, Task task) {String title = resultItems.get("title");System.out.println("我的定制的 title:"+title);}
}

2⃣️ 修改 main 方法

    //主函数,执行爬虫public static void main(String[] args) {Spider.create(new JobProcessor()).addUrl("https://blog.csdn.net/nav/ai")  //设置爬取数据的页面
//                .addPipeline(new ConsolePipeline())
//                .addPipeline(new FilePipeline("/Users/yangshuo/jd/")) //设置结果输出到文件.addPipeline(new JsonFilePipeline("/Users/yangshuo/jd/")) //以 JSON 方式保存.addPipeline(new MyPipeline())//定制化输出.thread(5)                               //设置5个线程执行.run();                                  //执行爬虫}

4 Spider 方法详解

方法 说明 示例
create(PageProcessor) 创建Spider Spider.create(new GithubRepoProcessor())
addUrl(String…) 添加初始的URL spider.addUrl("http://webmagic.io/docs/")
thread(n) 开启n个线程 spider.thread(5)
run() 启动,会阻塞,当前线程执行 spider.run()
start()/runAsync() 异步启动,当前线程继续执行 spider.start()
stop() 停止爬虫 spider.stop()
addPipeline(Pipeline) 添加一个Pipeline,一个Spider可以有多个Pipeline spider .addPipeline(new ConsolePipeline())
setScheduler(Scheduler) 设置Scheduler,一个Spider只能有个一个Scheduler spider.setScheduler(new RedisScheduler())
setDownloader(Downloader) 设置Downloader,一个Spider只能有个一个Downloader spider .setDownloader( new SeleniumDownloader())
get(String) 同步调用,并直接取得结果 ResultItems result = spider.get("http://webmagic.io/docs/")
getAll(String…) 同步调用,并直接取得一堆结果 List results = spider.getAll("http://webmagic.io/docs/","http://webmagic.io/xxx")

同时Spider的其他组件(Downloader、Scheduler、Pipeline)都可以通过set方法来进行设置。

Page代表了从Downloader下载到的一个页面——可能是HTML,也可能是JSON或者其他文本格式的内容。Page是WebMagic抽取过程的核心对象,它提供一些方法可供抽取、结果保存等。

爬虫配置 Site

Site用于定义站点本身的一些配置信息,例如编码、HTTP头、超时时间、重试策略等、代理等,都可以通过设置Site对象来进行配置。

方法 说明 示例
setCharset(String) 设置编码 site.setCharset("utf-8")
setUserAgent(String) 设置UserAgent site.setUserAgent("Spider")
setTimeOut(int) 设置超时时间, 单位是毫秒 site.setTimeOut(3000)
setRetryTimes(int) 设置重试次数 site.setRetryTimes(3)
setCycleRetryTimes(int) 设置循环重试次数 site.setCycleRetryTimes(3)
addCookie(String,String) 添加一条cookie site.addCookie("dotcomt_user","code4craft")
setDomain(String) 设置域名,需设置域名后,addCookie才可生效 site.setDomain("github.com")
addHeader(String,String) 添加一条addHeader site.addHeader("Referer","https://github.com")
setHttpProxy(HttpHost) 设置Http代理 site.setHttpProxy(new HttpHost("127.0.0.1",8080))

5 Scheduler

我们刚才完成的功能,每次运行可能会爬取重复的页面,这样做是没有任何意义的。

Scheduler(URL管理) 最基本的功能是实现对已经爬取的URL进行标示。可以实现URL的增
量去重。

目前scheduler主要有三种实现方式:

  • 内存队列 QueueScheduler
  • 文件队列 FileCacheQueueScheduler
    使用文件保存抓取URL,可以在关闭程序并下次启动时,从之前抓取到的URL继续抓取
  • Redis队列 RedisScheduler
    使用Redis保存抓取队列,可进行多台机器同时合作抓取

使用 setScheduler 来设置Scheduler

    //主函数,执行爬虫public static void main(String[] args) {Spider.create(new JobProcessor()).addUrl("https://blog.csdn.net/nav/ai")  //设置爬取数据的页面
//                .addPipeline(new ConsolePipeline())
//                .addPipeline(new FilePipeline("/Users/yangshuo/jd/")) //设置结果输出到文件.addPipeline(new JsonFilePipeline("/Users/yangshuo/jd/")) //以 JSON 方式保存.thread(5)                               //设置5个线程执行
//                .setScheduler(new QueueScheduler())      //设置内存队列
//                .setScheduler(new FileCacheQueueScheduler("/Users/yangshuo/jd/")) //设置文件队列.setScheduler(new RedisScheduler("127.0.0.1")) //设置Redis队列.run();                                  //执行爬虫}

9 WebMagic 入门案例相关推荐

  1. WebMagic入门案例

    WebMagic总体架构图如下: 项目结构图 配置文件 pom.xml <?xml version="1.0" encoding="UTF-8"?> ...

  2. day75,爬虫02,webmagic入门程序,组件介绍:Downloader,PageProcess,pipeline,Scheduler,51jop招聘网站综合案例

    一.webmagic入门程序(原理图) 使用方法 1)创建工程 2)添加jar包 <dependencies><!--WebMagic--><dependency> ...

  3. 2021年大数据Flink(八):Flink入门案例

    目录 Flink入门案例 前置说明 API 编程模型 准备工程 pom文件 log4j.properties Flink初体验 需求 编码步骤 代码实现 Flink入门案例 前置说明 API API ...

  4. Python:Scrapy的安装和入门案例

    Scrapy的安装介绍 Scrapy框架官方网址:http://doc.scrapy.org/en/latest Scrapy中文维护站点:http://scrapy-chs.readthedocs. ...

  5. Vue安装配置以及入门案例

    Vue Vue简介 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不 ...

  6. AOP配置开发入门案例

    该AOP开发入门案例采用XML文件方式配置开发(非注解方式)共包含一个xml文件和4个Java类,创建好web工程后引入相应jar包(文末会给出),建好包(若自定义包名注意更改类中的包名),将xml文 ...

  7. python与人工智能编程-总算明白python人工智能编程入门案例

    Python是非常简洁的一种脚本语言,写同样的程序,代码量仅为java的三分一,除了性能没有Java强之外,它的优点还是相当多的.以下是小编为你整理的python人工智能编程入门案例 下载Active ...

  8. mybatis基于注解的入门案例

    mybatis基于注解的入门案例:             把IUserDao.xml移除,在dao接口的方法上使用@Select注解,并且指定SQL语句             同时需要在SqlMa ...

  9. python截图保存到内存卡_Python画月饼,云上过中秋,天池Python入门案例系列赛开启...

    阿里云天池推出了一个Python入门案例系列教程,在此之前他们还推出了一个Python基础训练营. 在天池龙珠计划Python训练营中,天池给学习者详细的介绍了Python的基础和进阶知识,根据学习者 ...

最新文章

  1. 独家 | 带你认识几种最流行的Python编辑器/IDEs(附链接)
  2. 东北育才 数论专场第2场
  3. windows下hadoop的单机伪分布式部署(3)
  4. 关于Eclipes的Logcat无法打印消息的解决办法
  5. Linux中samba的权限详解,活用三种权限 理解Samba的权限控制
  6. 数据分析 数据清理_数据清理| 数据科学
  7. Python中的数组类型
  8. 甲骨文Java Archive
  9. Vue2.0三——Vue-router
  10. Linux 不同方法查看进程消耗CPU IO 等
  11. Dash for mac(代码文档浏览器)v6.0.8
  12. Hadoop高频面试题
  13. HTML静态网页作业-餐饮美食网页(HTML+CSS+JavaScript)
  14. Mac电脑DisplayPort/HDMI连接显示器后没声音
  15. TVM: End-to-End Optimization Stack for Deep Learning
  16. 在线文档方案---Google docs
  17. 域名申请步骤史上最全
  18. 23个shell实用脚本
  19. unity读取Texture文件并转为Sprit
  20. 宝塔安装php成功无显示,宝塔面板安装 LAMP 或 LNMP 后没有成功的原因

热门文章

  1. DSP关键字cregister (关于IER,IFR找不到定义)
  2. JAVASE IO流,文字不多,代码为主,自学用,谨慎借鉴,有错误请指正
  3. 《C++新经典Linux C++通信架构实战》第2章 进入Nginx之门
  4. 【STM32】芯片自锁、No Cortex-M SW Device Found/HardFault_Handler无法下载等问题的解决办法
  5. 【C++】基于固高驱动器的伺服电机项目调试
  6. 做“网站SEO”的你懂得分析搜索方向的大势吗?
  7. 小白入门IT不知道改选哪门技术?Java是你的不二之选
  8. Windows10系统重启后使用TEMP(临时)账户略解
  9. ubuntu图形化软件包管理工具synaptic
  10. 区别:秒s、毫秒ms、微秒μs、纳秒ns、皮秒ps、飞秒fs每两级之间的换算以及之间的关系