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.运行源码,得到结果:
分类:
WebCollector框架
标签:
WebCollector
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南