zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

WebCollector2.7爬虫框架——在Eclipse项目中配置详解编程语言

2023-06-13 09:11:43 时间
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.运行源码,得到结果:

WebCollector2.7爬虫框架——在Eclipse项目中配置详解编程语言

 

16719.html

cjava