`

深入学习Heritrix---解析处理器(Processor)

 
阅读更多

键字: heritrix processor

本节解析与处理器有关的内容.
与处理器有关的主要在以下几个类:Processor(处理器类),ProcessorChain(处理器类),ProcessorChainList(处理器链列表).它们之间的关系如下:
下面将解析该图.
(1)Processor
代表一个处理器.
 

package org.archive.crawler.framework;

public class Processor extends ModuleType {
//默认的下一个处理器
    private Processor defaultNextProcessor = null;
 
/**
     * Perform processing on the given CrawlURI.
     *    处理一个链接
     * 
@param curi
     * 
@throws InterruptedException
     
*/
    
public final void process(CrawlURI curi) throws InterruptedException {
        
// by default, arrange for curi to proceed to next processor
        
//设置当前处理器的下一个处理器
        curi.setNextProcessor(getDefaultNextProcessor(curi));

        
// Check if this processor is enabled before processing
        try {
            
if (!((Boolean) getAttribute(ATTR_ENABLED, curi)).booleanValue()) {
                
return;
            }
        } 
catch (AttributeNotFoundException e) {
            logger.severe(e.getMessage());
        }

        
if(rulesAccept(curi)) {
            innerProcess(curi); 
//留给子类实现
        } else {
            innerRejectProcess(curi);
        }
    }
 
 
 
(2)ProcessorChain
该类实际上实现一个队列的功能,它代表一个由许多处理器连接的处理器链.
 

package org.archive.crawler.framework;
public class ProcessorChain {
 
//存放当前处理链中所有的处理器
    private final MapType processorMap;
    
//下一个处理器链
    private ProcessorChain nextChain;
    
//处理器链的第一个处理器
    private Processor firstProcessor;
/** Construct a new processor chain.
     *    把该处理链的所有的处理器连接起来
     * 
@param processorMap a map of the processors belonging to this chain.
     
*/
    
public ProcessorChain(MapType processorMap) {
        
this.processorMap = processorMap;

        Processor previous 
= null;

        
for (Iterator it = processorMap.iterator(null); it.hasNext();) {
            Processor p 
= (Processor) it.next();

            
if (previous == null) {
                firstProcessor 
= p;
            } 
else {
                
//设置前一个处理器的下一个处理器为当前处理器
                previous.setDefaultNextProcessor(p);
            }

            logger.info(
                
"Processor: " + p.getName() + " --> " + p.getClass().getName());
            
//当前处理器设置为前一个处理器
            previous = p;
        }
    }

    
/** Set the processor chain that the URI should be working through after
     * finishing this one.
     * 设置下一个处理器
     * 
@param nextProcessorChain the chain that should be processed after this
     *        one.
     
*/
    
public void setNextChain(ProcessorChain nextProcessorChain) {
        
this.nextChain = nextProcessorChain;
    }

    
/** Get the processor chain that the URI should be working through after
     * finishing this one.
     *
     * 
@return the next processor chain.
     
*/
    
public ProcessorChain getNextProcessorChain() {
        
return nextChain;
    }

    
/** Get the first processor in the chain.
     * 获取第一个处理器
     * 
@return the first processor in the chain.
     
*/
    
public Processor getFirstProcessor() {
        
return firstProcessor;
    }
 
 
 
(3)ProcessorChainList
该类是保存一次抓取任务的所有的处理器链(ProcessorChain).
 

package org.archive.crawler.framework;
public class ProcessorChainList {
    
//处理器链列表,保存所有的处理器链
    private List<ProcessorChain> chainList = new ArrayList<ProcessorChain>();
    
//所有的处理器
    private Map<String,ProcessorChain> chainMap
     
= new HashMap<String,ProcessorChain>();

     
/** Add a new chain of processors to the chain list.
     * 将所有的处理器链添加到Map中
     * This method takes a map of processors and wraps it in a ProcessorChain
     * object and adds it to the list of chains.
     *
     * 
@param processorMap the processor map to be added.
     
*/
    
public void addProcessorMap(String name, MapType processorMap) {
        
//由MapType生成一个处理器链
        ProcessorChain processorChain = new ProcessorChain(processorMap);
        ProcessorChain previousChain 
= getLastChain();
        
if (previousChain != null) {
            
//设置下一个处理器链
            previousChain.setNextChain(processorChain);
        }
        chainList.add(processorChain);
        chainMap.put(name, processorChain);
    }

    
/** Get the first processor chain.
     * 获取第一个处理链
     * 
@return the first processor chain.
     
*/
    
public ProcessorChain getFirstChain() {
        
return (ProcessorChain) chainList.get(0);
    }

 
 (4)ToeThread
为了高效抓取网页,Heritrix采用了线程池的设计.每一个线程将调用所有的处理器来处理链接.
 

 private void processCrawlUri() throws InterruptedException {
        currentCuri.setThreadNumber(
this.serialNumber);
        
//获取第一个处理器链
        currentCuri.setNextProcessorChain(controller.getFirstProcessorChain());
        lastStartTime 
= System.currentTimeMillis();
//        System.out.println(currentCuri);
        try {
            
while (currentCuri.nextProcessorChain() != null) {
                setStep(STEP_ABOUT_TO_BEGIN_CHAIN);
                
// Starting on a new processor chain.
                
//设置下一个处理器
                currentCuri.setNextProcessor(currentCuri.nextProcessorChain().getFirstProcessor());
                currentCuri.setNextProcessorChain(currentCuri.nextProcessorChain().getNextProcessorChain());

                
while (currentCuri.nextProcessor() != null) {
                    setStep(STEP_ABOUT_TO_BEGIN_PROCESSOR);
                    Processor currentProcessor 
= getProcessor(currentCuri.nextProcessor());
                    currentProcessorName 
= currentProcessor.getName();
                    continueCheck();
//                    long memBefore = (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024;
                    
//调用处理器处理链接
                    currentProcessor.process(currentCuri);
//                    long memAfter = (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024;
//                    System.out.println((memAfter-memBefore)+"K in "+currentProcessorName);
                }
            }
            setStep(STEP_DONE_WITH_PROCESSORS);
            currentProcessorName 
= "";
        } 
catch (RuntimeExceptionWrapper e) {
            
// Workaround to get cause from BDB
            if(e.getCause() == null) {
                e.initCause(e.getCause());
            }
            recoverableProblem(e);
        } 
catch (AssertionError ae) {
            
// This risks leaving crawl in fatally inconsistent state, 
            
// but is often reasonable for per-Processor assertion problems 
            recoverableProblem(ae);
        } 
catch (RuntimeException e) {
            recoverableProblem(e);
        } 
catch (StackOverflowError err) {
            recoverableProblem(err);
        } 
catch (Error err) {
            
// OutOfMemory and any others
            seriousError(err); 
        }
 
 
(5)处理器链的初始化
所有的处理器链都是在CrawlController的initialize中初始化的.
 

 public void initialize(SettingsHandler sH)
{

 
//初始化了Scope、Frontier以及ProcessorChain
            setupCrawlModules();

 
 
 

private void setupCrawlModules(){

        
//设置处理链
        
// Setup processors
        if (processorChains == null) {
            processorChains 
= new ProcessorChainList(order);
        }
分享到:
评论

相关推荐

    heritrix-3.4.0-SNAPSHOT-src.zip

    Heritrix 是一个由 java 开发的、开源的网络爬虫,用户可以使用它来从网上抓取想要的资源。官网下载好像要翻墙,我下下来方便大家使用,这是3.4版本,配合heritrix-3.4.0-SNAPSHOT-dist.zip使用

    heritrix-1.14.4.zip 和 heritrix-1.14.4-src.zip

    Heritrix 1.14.4是该工具的一个版本,提供了两个压缩包:`heritrix-1.14.4.zip`和`heritrix-1.14.4-src.zip`。这两个文件分别包含了不同的内容,便于用户根据需求进行使用和开发。 `heritrix-1.14.4.zip` 包含了...

    heritrix-1.14.4-src

    总的来说,Heritrix-1.14.4-src提供了深入了解网络爬虫工作原理的机会,同时也让用户有机会自定义和优化爬虫行为,以满足特定的业务需求。虽然这个版本可能没有最新版的特性,但对于学习和理解爬虫技术来说,仍然是...

    Heritrix-1.4.4.src.zip +Heritrix-1.4.4.zip

    在提供的压缩包中,有两个主要文件:"heritrix-1.14.4.zip" 和 "heritrix-1.14.4-src.zip"。前者是Heritrix的编译后的二进制版本,可以直接运行,而后者包含了源代码,对于希望定制或深入理解Heritrix工作原理的...

    heritrix-1.14.4

    这个"heritrix-1.14.4"版本是Heritrix的特定发行版,提供了对互联网资源进行系统性抓取的功能,帮助用户构建自己的网络存档。 标题"heritrix-1.14.4"表明这是Heritrix的1.14.4版本,这是一个重要的标识,因为每个...

    Heritrix-1.14.4源代码

    Heritrix-1.14.4源代码的提供,对于那些希望深入理解网络爬虫工作原理、想要定制爬虫功能或者进行相关研究的开发者来说,是一个宝贵的学习资源。 Heritrix的设计遵循模块化和可扩展的原则,它将爬虫的功能分解为多...

    Heritrix-3.1.0(linux版)

    - **模块化架构**:Heritrix的设计基于模块化,允许用户添加或替换各种组件,如爬取管道中的处理器、解析器和存储模块。 - **爬取策略**:用户可以根据需求定制爬取策略,例如按照时间戳、URL结构或内容类型进行选择...

    heritrix-1.12.1

    3. **内容处理器**:这些组件负责解析和处理抓取到的网页内容,例如提取链接、识别元数据等。 4. **存储机制**:Heritrix支持多种存储选项,如文件系统、数据库或自定义存储解决方案,用于保存抓取的数据。 5. **...

    heritrix-3.1.0 最新jar包

    1. **heritrix-3.1.0-dist.zip**:这是Heritrix的发行版,包含运行所需的所有文件,如Java可执行文件(JARs)、配置文件和文档。用户可以直接下载并运行此版本来启动爬虫服务,无需构建源代码。其中,`heritrix-...

    heritrix-3.4.0-SNAPSHOT-dist.zip

    - `heritrix-3.4.0-SNAPSHOT`目录:这是Heritrix的主目录,包含了所有运行所需的基本文件,如jar包、配置文件、文档等。 - `bin`子目录:存放启动和停止Heritrix的脚本,通常在Unix/Linux环境下使用`start.sh`和`...

    heritrix-1.12.1-src.zip与heritrix 配置文档

    heritrix-1.12.1-src.zip与heritrix 配置文档

    heritrix-1.14.4-docs.rar

    "heritrix-1.14.4-docs.rar"这个压缩包包含了该版本的文档,帮助用户理解和使用Heritrix。 文档通常包括用户手册、开发者指南、API参考等,这些内容对于熟悉Heritrix的架构、配置和编程接口至关重要。由于文件较大...

    heritrix-1.14.4控制台可执行版本

    近期需要使用heritrix-1.14.4,配了半天才配好,这个是控制台执行版本. 注意:解压到相关目录,之后配置系统环境变量"HERITRIX_HOME"到该解压目录(Java环境已经配置好)。 使用控制台命令启动 : heritrix --admin=...

    heritrix-1.14.3-src.zip

    "heritrix-1.14.3-src.zip"是一个包含了Heritrix 1.14.3版本源代码的压缩文件,对于那些希望深入理解其工作原理或者想要自定义功能的开发者来说,这是一个宝贵的资源。 Heritrix的核心设计基于模块化架构,允许...

    heritrix-1.14.2.zip

    Heritrix的压缩包"heritrix-1.14.2.zip"包含以下组件和文件: 1. **源代码**:包含了Heritrix的Java源代码,用户可以查看和修改这些代码以适应自己的需求。 2. **构建脚本**:如Ant或Maven脚本,用于编译和打包项目...

    heritrix-1.12.1.zip

    2. **模块化设计**:Heritrix采用模块化架构,包含启动器(Launcher)、队列管理器(Queue Manager)、URI处理器(URI Processor)、下载器(Fetcher)、解析器(Parser)和存储器(Store)。每个模块都有特定的任务...

    heritrix-1.14.4爬虫框架及源码

    这个版本的源码和编译后的二进制文件分别以"heritrix-1.14.4.zip"和"heritrix-1.14.4-src.zip"的名义提供,允许用户进行深入研究、学习或二次开发。 在Heritrix中,爬虫的主要工作流程包括种子管理、URL过滤、内容...

    heritrix-1.14.4 for linux

    标题"heritrix-1.14.4 for linux"表明这是Heritrix的Linux兼容版本,版本号为1.14.4。在Linux操作系统上运行Heritrix,用户可以利用Linux系统的稳定性和高效性来处理大量的网络抓取任务。 描述中的"heritrix-1.14.4...

    heritrix-1.14.0-src

    在解压后的`heritrix-1.14.0`目录中,通常会包含以下结构: - `src` 目录:存放源代码文件,包括主要的Java类和配置文件。 - `build.xml`:Ant构建文件,用于编译和打包项目。 - `README` 和 `LICENSE` 文件:提供...

    Heritrix-User-Manual.rar_heritrix

    这个名为"Heritrix-User-Manual.rar_heritrix"的压缩包包含了Heritrix用户手册的PDF版本,是学习和操作Heritrix的重要资源。下面将详细介绍Heritrix的基本概念、安装步骤、任务创建以及任务分析。 1. **Heritrix...

Global site tag (gtag.js) - Google Analytics