WebCollector下载整站页面(JAVA网络爬虫)
来源:互联网 发布:象棋高手学软件 编辑:程序博客网 时间:2024/05/22 01:24
很多业务需要下载整站页面(有时为多个站点),将页面按照网站拓扑结构存放。
下面给出用JAVA爬虫WebCollector(2.09版本以上)爬取整站网页并按照网页拓扑结构存储到本地的代码。
代码中的抽取器可以作为一个插件复用。
WebCollector的jar包可到官网下载:WebCollector官网。进入官网后下载webcollector-版本号-bin.zip,解压得到所需jar包。
import cn.edu.hfut.dmic.webcollector.crawler.MultiExtractorCrawler;import cn.edu.hfut.dmic.webcollector.extract.Extractor;import cn.edu.hfut.dmic.webcollector.extract.ExtractorParams;import cn.edu.hfut.dmic.webcollector.model.Page;import cn.edu.hfut.dmic.webcollector.util.FileSystemOutput;import cn.edu.hfut.dmic.webcollector.util.FileUtils;import java.io.File;/** * Created by hu on 2015/6/25. */public class HtmlExtractor extends Extractor{ FileSystemOutput fsOutput; public HtmlExtractor(Page page, ExtractorParams params) { super(page, params); /*每次抽取都会实例化一个Extractor对象,为了让所有Extractor对象共享一个FileSystemOutput对象, 在外部实例化一个FileSystemOutput对象fsOutput,以参数形式传给每个Extractor对象,这里是获取 外部传入的FileSystemOutput对象*/ fsOutput= (FileSystemOutput) params.get("fsOutput"); } @Override public boolean shouldExecute() { //我们希望对所有网页执行此抽取器 return true; } @Override public void extract() throws Exception { //本程序不需要进行网页抽取,所以extract()方法中不需要插入代码 } @Override public void output() throws Exception { fsOutput.output(page); } public static void main(String[] args) throws Exception { /*如果下载文件夹存在,先删除文件夹*/ File downloadDir=new File("download"); if(downloadDir.exists()){ FileUtils.deleteDir(downloadDir); } FileSystemOutput fsOutput=new FileSystemOutput("download"); MultiExtractorCrawler crawler=new MultiExtractorCrawler("crawl",true); crawler.addSeed("http://36kr.com/"); crawler.addRegex("http://36kr.com/.*"); crawler.addExtractor(".*", HtmlExtractor.class, new ExtractorParams("fsOutput",fsOutput)); crawler.start(100); }}
程序执行后可到download文件夹中查看保存的网页:
3 0
- WebCollector下载整站页面(JAVA网络爬虫)
- JAVA网络爬虫WebCollector深度解析——爬虫内核
- JAVA网络爬虫WebCollector深度解析——爬虫内核
- Java爬虫-WebCollector
- JAVA爬虫 WebCollector
- JAVA爬虫WebCollector
- JAVA爬虫 WebCollector
- Java之网络爬虫WebCollector+selenium+phantomjs(一)
- Java之网络爬虫WebCollector+selenium+phantomjs(二)
- Java之网络爬虫WebCollector+selenium+phantomjs(三)
- Java之网络爬虫WebCollector+selenium+phantomjs(一)
- Java之网络爬虫WebCollector+selenium+phantomjs(二)
- Java之网络爬虫WebCollector+selenium+phantomjs(三)
- JAVA爬虫WebCollector教程列表
- WebCollector java爬虫使用笔记
- WebCollector java爬虫使用笔记
- Java WebCollector爬虫采集数据
- java网络爬虫——下载页面图片
- Python中find()方法的使用
- UFLDL(新版)中文翻译——Supervised Learning and Optimization: Logistic Regression
- PHP设计模式
- 环境烘培与shader初步,学习笔记一
- 学习H5+CSS3+JS的一些心得体会
- WebCollector下载整站页面(JAVA网络爬虫)
- 第十六周周四GUI编程及文件及选择
- 设置软键盘搜索键以及监听搜索键点击
- GitLab常用命令
- Android 使用notepad++ 格式化本地Json数据需要的对比安装插件
- JS请求JSON格式的数据及JSON对象
- 拉电流、灌电流、吸电流、上下拉电阻和高阻态
- Java多个线程之间处理共享数据的方式
- Machine Learning Books Suggested by Michael I. Jordan from Berkeley