zoukankan      html  css  js  c++  java
  • WebMagic

    WebMagic 是干嘛的?

    WebMagic 是一个 Java 平台上的开源爬虫框架,其设计参考了 Scrapy,实现则参考了 HttpClient 和 Jsoup。其由四大组件组成:

    1. Downloader,负责下载网页,使用 HttpClient。
    2. PageProcessor,负责解析网页和链接发现,使用 Jsoup 和 Xsoup。
    3. Scheduler,负责管理待抓取的 URL 和去重。
    4. Pipeline,负责结果数据的持久化。

    快速开始

    (1)依赖引入

    ext {
      versions = [
        "web_magic": '0.7.3'
      ]
    }
    
    dependencies {
      // 这里有自己项目的日志实现
      compile project(':base')
    
      compile("us.codecraft:webmagic-core:${versions.web_magic}") {
        exclude group: 'org.slf4j', module: 'slf4j-log4j12' // 移除默认的日志实现
      }
      compile("us.codecraft:webmagic-extension:${versions.web_magic}") {
        exclude group: 'org.slf4j', module: 'slf4j-log4j12'
      }
    }
    

    (2)快速开始

    爬取 https://github.com/code4craft/ 页面上可以发现的所有 Github 仓库信息。

    
    public class GithubRepoPageProcessor implements PageProcessor {
    
      private Site site = Site.me().setRetryTimes(3).setSleepTime(200).setTimeOut(10000);
    
      @Override
      public void process(Page page) {
        String regex = "(https://github\.com/code4craft/([\w-_]+)/)";
        page.addTargetRequests(page.getHtml()
                                   .links()
                                   .regex(regex)
                                   .all());
        if(!Pattern.matches(regex,page.getUrl().get())){
          //skip this page
          page.setSkip(true);
        }
        page.putField("author", page.getUrl().regex("https://github\.com/(\w+)/.*").toString());
        page.putField("name",
                      page.getHtml()
                          .xpath("//meta[@property='og:title']/@content")
                          .toString());
        if (page.getResultItems().get("name") == null) {
          page.setSkip(true);
        }
    //    page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
      }
    
      @Override
      public Site getSite() {
        return site;
      }
    
      public static void main(String[] args) {
        Spider.create(new GithubRepoPageProcessor())
              .addUrl("https://github.com/code4craft/")
              .thread(5)
              .run();
      }
    }
    

    更进一步

    Pipeline 接口参数分析

    Pipeline 接口会在每个 Page 解析完成之后回调一次。其中的参数如下:

    (1)Task

    {
        "exitWhenComplete": true,
        "pageCount": 0, // 抓取的第几页
        "scheduler": {
            "duplicateRemover": {}
        },
        "site": {
            "acceptStatCode": [
                200
            ],
            "allCookies": {},
            "cookies": {},
            "cycleRetryTimes": 0,
            "disableCookieManagement": false,
            "domain": "github.com",
            "headers": {
                ":method": "GET",
                "origin": "https://github.com"
            },
            "retrySleepTime": 1000,
            "retryTimes": 3,
            "sleepTime": 100,
            "timeOut": 10000,
            "useGzip": true
        },
        "spawnUrl": true,
        "startTime": 1544165065094,
        "status": "Running",
        "threadAlive": 1,
        "uUID": "github.com"
    }
    

    (2)ResultItems

    {
        "all": {
            // 自定义的字段在这里
            "a_key":"a_value"
        },
        "request": {
            "binaryContent": false,
            "cookies": {},
            "headers": {},
            "priority": 0,
            "url": "https://github.com/code4craft?tab=repositories"
        },
        "skip": false
    }
    

    排错

    Https下无法抓取只支持TLS1.2的站点

    作者 code4craft 针对 ISSUE 701 提供了如下的解决方案:

    更新会在0.7.4版本发布。
    
    临时适配方式,修改HttpClientGenerator中的buildSSLConnectionSocketFactory方法,
    
    return new SSLConnectionSocketFactory(createIgnoreVerifySSL(), new String[]{"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"},
                        null,
                        new DefaultHostnameVerifier())
    重写自己实现的HttpClientDownloader,并设置到Spider中。
    

    java.net.UnknownHostException

    请检查网络连接。

    参考

    1. WebMagic in Action - webmagic.io
  • 相关阅读:
    mouse_event模拟鼠标滚轮
    润乾报表配置技术路线
    建筑 物件 开心背单词 读句子,单词,字母,看图例, 翻译,看动画
    文字过渡动画,曲线过渡动画,,使用这个插件assign shape keys
    运动锻炼 开心背单词 读句子,单词,字母,看图例, 翻译,看动画,学英语,轻松背单词,简单背单词
    blender293 内置插件 精度绘画控件,PDT学习003,pdt tangents 切线
    日常用品 背单词 读句子 看图片 读单词 读字母 翻译, 看动画 学英语
    blender293 内置插件 精度绘画控件,PDT学习 precision drawing tools
    乔布斯 背单词 02 读句子 单词 字母 翻译,看动画 学英语 名言 我菜顾我在,我菜故我在,blender加python
    狐狸 和 乌鸦 英语 朗读句子 背单词
  • 原文地址:https://www.cnblogs.com/lshare/p/11334698.html
Copyright © 2011-2022 走看看