2. 快速开始

WebMagic主要包含两个jar包:webmagic-core-{version}.jarwebmagic-extension-{version}.jar。在项目中添加这两个包的依赖,即可使用WebMagic。

WebMagic默认使用Maven管理依赖,但是你也可以不依赖Maven进行使用。

2.1 使用Maven

WebMagic基于Maven进行构建,推荐使用Maven来安装WebMagic。在你自己的项目(已有项目或者新建一个)中添加以下坐标即可:

  1.  
    <dependency>
  2.  
    <groupId>us.codecraft</groupId>
  3.  
    <artifactId>webmagic-core</artifactId>
  4.  
    <version>0.7.3</version>
  5.  
    </dependency>
  6.  
    <dependency>
  7.  
    <groupId>us.codecraft</groupId>
  8.  
    <artifactId>webmagic-extension</artifactId>
  9.  
    <version>0.7.3</version>
  10.  
    </dependency>

WebMagic使用slf4j-log4j12作为slf4j的实现.如果你自己定制了slf4j的实现,请在项目中去掉此依赖。

  1.  
    <dependency>
  2.  
    <groupId>us.codecraft</groupId>
  3.  
    <artifactId>webmagic-extension</artifactId>
  4.  
    <version>0.7.3</version>
  5.  
    <exclusions>
  6.  
    <exclusion>
  7.  
    <groupId>org.slf4j</groupId>
  8.  
    <artifactId>slf4j-log4j12</artifactId>
  9.  
    </exclusion>
  10.  
    </exclusions>
  11.  
    </dependency>
     

    在你的项目中添加了WebMagic的依赖之后,即可开始第一个爬虫的开发了!我们这里拿一个抓取Github信息的例子:

    1.  
      import us.codecraft.webmagic.Page;
    2.  
      import us.codecraft.webmagic.Site;
    3.  
      import us.codecraft.webmagic.Spider;
    4.  
      import us.codecraft.webmagic.processor.PageProcessor;
    5.  
       
    6.  
      public class GithubRepoPageProcessor implements PageProcessor {
    7.  
       
    8.  
      private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
    9.  
       
    10.  
      @Override
    11.  
      public void process(Page page) {
    12.  
      page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
    13.  
      page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString());
    14.  
      page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
    15.  
      if (page.getResultItems().get("name")==null){
    16.  
      //skip this page
    17.  
      page.setSkip(true);
    18.  
      }
    19.  
      page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
    20.  
      }
    21.  
       
    22.  
      @Override
    23.  
      public Site getSite() {
    24.  
      return site;
    25.  
      }
    26.  
       
    27.  
      public static void main(String[] args) {
    28.  
      Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
    29.  
      }
    30.  
      }

    点击main方法,选择“运行”,你会发现爬虫已经可以正常工作了!

最新文章

  1. ASP.NET Core 运行原理剖析1:初始化WebApp模版并运行
  2. 推荐15款最好的 Twitter Bootstrap 开发工具
  3. nodejs学习篇 (1)webstorm创建nodejs + express + jade 的web 项目
  4. Theano2.1.12-基础知识之使用GPU
  5. iOS 在UILabel显示不同的字体和颜色(转)
  6. JMeter学习-006-Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002 解决办法(windows 7)
  7. css中transition的使用以及:before:after的使用(小样式)
  8. javascript 返回数组中不重复的元素
  9. Java基础-事件处理
  10. CSS width:100%和width:auto的区别
  11. Android:创建Android工程
  12. Tar打包、压缩与解压缩到指定目录的方法
  13. automake/autoconf的简单例子
  14. 1.WF 4.5在项目中直接使用的问题
  15. 网络编程一定要看过的socket大山
  16. Manjaro使用笔记-使用中国源的方法
  17. ASP.Net Core 2.2 MVC入门到基本使用系列 (四)(转)
  18. Makefile依赖关系中的竖线“|”
  19. input file 文件上传标签的样式美化
  20. Applese走迷宫-bfs

热门文章

  1. GO学习之 从hello.go开始
  2. CF-----Pillars
  3. 一个汇编小demo
  4. Python日期
  5. js下false情况
  6. MongoDB-1 入门
  7. Informatica PowerCenter 常用转换组件一览表
  8. POJ3264 Balances Lineup
  9. Spring Boot 2.x 入门前的准备-安装 Java JDK
  10. Python基础模块整理