zoukankan      html  css  js  c++  java
  • WebCollector2.7爬虫框架——在Eclipse项目中配置

    WebCollector2.7爬虫框架——在Eclipse项目中配置

    在Eclipse项目中使用WebCollector爬虫非常简单,不需要任何其他的配置,只需要导入相关的jar包即可。

    Netbeans、Intellij也是非常优秀的IDE,下面的方法也同样适用于Netbeans和Intellij(有细微差别),推荐使用Netbeans或Intellij。至于Netbeans和Intellij的项目结构是否通用这个问题,其实是不用考虑的,因为Eclipse项目结构也是不通用的,参与过开源软件开发的人应该知道,apache等开源组织发布的源码往往是ant项目或maven项目,这些才是通用的项目结构,并且Netbeans和Intellij在对ant和maven的支持上,比Eclipse好得多。

    动手配置项目:

    具体步骤如下:

    1.采用Maven:

    <dependency>
        <groupId>cn.edu.hfut.dmic.webcollector</groupId>
        <artifactId>WebCollector</artifactId>
        <version>2.72</version>
    </dependency>

    如果不使用Maven,可以进入WebCollector官方网站下载最新版本所需jar包。

    最新版本的jar包放在webcollector-version-bin.zip中。

    2.打开Eclipse,选择File->New->Java Project,按照正常步骤新建一个JAVA项目。

    在工程根目录下新建一个文件夹lib,将刚下载的webcollector-version-bin.zip解压后得到的所有jar包放到lib文件夹下。将jar包放到build path中。

    3.现在可以编写WebCollector爬虫的代码了,例如我们编写一个爬取网站图片例子。

    新建一个类DemoImageCrawler.java,源码如下:

    /** 
     * Project Name:webcllector 
     * File Name:NewsCrawler.java 
     * Package Name:webcllector 
     * Date:2018年7月25日下午2:26:50 
     * Copyright (c) 2018 All Rights Reserved. 
     * 
    */  
      
    package webcllector;  
    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;
    import cn.edu.hfut.dmic.webcollector.plugin.net.OkHttpRequester;
    import cn.edu.hfut.dmic.webcollector.util.ExceptionUtils;
    import cn.edu.hfut.dmic.webcollector.util.FileUtils;
    import cn.edu.hfut.dmic.webcollector.util.MD5Utils;
    
    import java.io.File;
    
    
    public class DemoImageCrawler extends BreadthCrawler {
        File baseDir = new File("images");
        /**
         * 构造一个基于伯克利DB的爬虫
         * 伯克利DB文件夹为crawlPath,crawlPath中维护了历史URL等信息
         * 不同任务不要使用相同的crawlPath
         * 两个使用相同crawlPath的爬虫并行爬取会产生错误
         *
         * @param crawlPath 伯克利DB使用的文件夹
         */
        public DemoImageCrawler(String crawlPath) {
            super(crawlPath, true);
    
            //只有在autoParse和autoDetectImg都为true的情况下
            //爬虫才会自动解析图片链接
            getConf().setAutoDetectImg(true);
    
            //如果使用默认的Requester,需要像下面这样设置一下网页大小上限
            //否则可能会获得一个不完整的页面
            //下面这行将页面大小上限设置为10M
            //getConf().setMaxReceiveSize(1024 * 1024 * 10);
    
            //添加种子URL
            addSeed("http://www.meishij.net/");
            //限定爬取范围
            addRegex("http://www.meishij.net/.*");
            addRegex("http://images.meishij.net/.*");
            addRegex("-.*#.*");
            addRegex("-.*\?.*");
            //设置为断点爬取,否则每次开启爬虫都会重新爬取
    //        demoImageCrawler.setResumable(true);
            setThreads(30);
    
        }
    
        @Override
        public void visit(Page page, CrawlDatums next) {
            //根据http头中的Content-Type信息来判断当前资源是网页还是图片
            String contentType = page.contentType();
            //根据Content-Type判断是否为图片
            if(contentType!=null && contentType.startsWith("image")){
                //从Content-Type中获取图片扩展名
                String extensionName=contentType.split("/")[1];
                try {
                    byte[] image = page.content();
                    //根据图片MD5生成文件名
                    String fileName = String.format("%s.%s",MD5Utils.md5(image), extensionName);
                    File imageFile = new File(baseDir, fileName);
                    FileUtils.write(imageFile, image);
                    System.out.println("保存图片 "+page.url()+" 到 "+ imageFile.getAbsolutePath());
                } catch (Exception e) {
                    ExceptionUtils.fail(e);
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
            DemoImageCrawler demoImageCrawler = new DemoImageCrawler("crawl");
            demoImageCrawler.setRequester(new OkHttpRequester());
            //设置为断点爬取,否则每次开启爬虫都会重新爬取
            demoImageCrawler.setResumable(true);
            demoImageCrawler.start(3);
        }
    }

    4.运行源码,得到结果:

  • 相关阅读:
    Grid表格的js触发事件
    C# 在获得鼠标点击事件时,如何判断Control键,Shift键被按下
    纠错《COM技术内幕》之ProgID
    C# 日期格式化
    C# 操作系统防火墙
    C# 开发和调用Web Service
    谓侠
    高维FWT
    单位根反演
    容斥 反演
  • 原文地址:https://www.cnblogs.com/lizm166/p/9365691.html
Copyright © 2011-2022 走看看