大约nutch基础知识可以参考lemo柱

nutch支持二次开发,为了满足搜索的准确性的问题,内容提取出来作为索引的内容,相应的是parse_text的数据。我使用的事nutch1.4 版本号,在cygwin下运行crawl命令进行爬取。

bin/nutch crawl urls -dir crawl -depth 3 -topN 30

爬取的流程例如以下:inject :将urls下的url文档中的url注入到数据库。generate:从数据库中取得url获取须要爬取的url队列。fetch:从url爬取队列中爬取page,parse:解析page的内容。

从这里看到我须要改写的是parse对网页解析部分,parse对网页进行解析后将解析的text放入crawl/segments下相应的parse_text目录下,我们能够通过命令

bin/nutch readseg -dump crawl/segments/20120710142020 segdata

查看详细爬取的内容。

从系统的扩展点,通过实现系统中的parser扩展点,就可以实现自己的parse应用,而系统中对html页面解析是通过默认的parse-html插件实现的,这里我们为了方便(但升级nutch版本号之后就不方便了),直接在parse-html插件处进行改动。

首先我们先找到parse-html实现parser借口的getparse方法,这种方法是详细解析网页内容的。

public ParseResult getParse(Content content) {
HTMLMetaTags metaTags = new HTMLMetaTags(); URL base;
try {
base = new URL(content.getBaseUrl());
} catch (MalformedURLException e) {
return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf());
} String text = "";
String title = "";
Outlink[] outlinks = new Outlink[0];
Metadata metadata = new Metadata(); // parse the content
DocumentFragment root;
try {
byte[] contentInOctets = content.getContent();
InputSource input = new InputSource(new ByteArrayInputStream(contentInOctets)); EncodingDetector detector = new EncodingDetector(conf);
detector.autoDetectClues(content, true);
detector.addClue(sniffCharacterEncoding(contentInOctets), "sniffed");
String encoding = detector.guessEncoding(content, defaultCharEncoding); metadata.set(Metadata.ORIGINAL_CHAR_ENCODING, encoding);
metadata.set(Metadata.CHAR_ENCODING_FOR_CONVERSION, encoding); input.setEncoding(encoding);
if (LOG.isTraceEnabled()) { LOG.trace("Parsing..."); }
root = parse(input);
} catch (IOException e) {
return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf());
} catch (DOMException e) {
return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf());
} catch (SAXException e) {
return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf());
} catch (Exception e) {
e.printStackTrace(LogUtil.getWarnStream(LOG));
return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf());
} // get meta directives
HTMLMetaProcessor.getMetaTags(metaTags, root, base);
if (LOG.isTraceEnabled()) {
LOG.trace("Meta tags for " + base + ": " + metaTags.toString());
}
// check meta directives
if (!metaTags.getNoIndex()) { // okay to index
StringBuffer sb = new StringBuffer();
if (LOG.isTraceEnabled()) { LOG.trace("Getting text..."); }
try {
utils.getText(sb, root);// 这里是详细解析text的位置
text = sb.toString();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sb.setLength(0);
if (LOG.isTraceEnabled()) { LOG.trace("Getting title..."); }
utils.getTitle(sb, root); // extract title
title = sb.toString().trim();
} if (!metaTags.getNoFollow()) { // okay to follow links
ArrayList<Outlink> l = new ArrayList<Outlink>(); // extract outlinks
URL baseTag = utils.getBase(root);
if (LOG.isTraceEnabled()) { LOG.trace("Getting links..."); }
utils.getOutlinks(baseTag!=null?baseTag:base, l, root);
outlinks = l.toArray(new Outlink[l.size()]);
if (LOG.isTraceEnabled()) {
LOG.trace("found "+outlinks.length+" outlinks in "+content.getUrl());
}
} ParseStatus status = new ParseStatus(ParseStatus.SUCCESS);
if (metaTags.getRefresh()) {
status.setMinorCode(ParseStatus.SUCCESS_REDIRECT);
status.setArgs(new String[] {metaTags.getRefreshHref().toString(),
Integer.toString(metaTags.getRefreshTime())});
}
ParseData parseData = new ParseData(status, title, outlinks,
content.getMetadata(), metadata);
ParseResult parseResult = ParseResult.createParseResult(content.getUrl(),
new ParseImpl(text, parseData)); // run filters on parse
ParseResult filteredParse = this.htmlParseFilters.filter(content, parseResult,
metaTags, root);
if (metaTags.getNoCache()) { // not okay to cache
for (Map.Entry<org.apache.hadoop.io.Text, Parse> entry : filteredParse)
entry.getValue().getData().getParseMeta().set(Nutch.CACHING_FORBIDDEN_KEY,
cachingPolicy);
}
return filteredParse;
}

我们从代码中能够看到详细解析text的位置,我们须要改动的就是这个位置的代码了,能够通过查看源码,nutch是 通过Dom tree的方式进行解析text内容的,而我在这里为了拿到page的正文部分的内容,我选用了开源的工具boilerpipe进行正文的提取。插入如上函数的代码段为:

text = BoilerpipeUtils.getMainbodyTextByBoilerpipe(new InputSource(
new ByteArrayInputStream(content.getContent())));
if(text.equals("")){
utils.getText(sb, root);
text = sb.toString();
if (LOG.isTraceEnabled()) {
LOG.trace("Extract text using DOMContentUtils...");
}
}else if (LOG.isTraceEnabled()) {
LOG.trace("Extract text using Boilerpipe...");
}
FileWriter fw = new FileWriter("E://mainbodypage//URLText.txt",true);
fw.write("url::" + content.getUrl() + "\n");
fw.write("text::" + text + "\n");
fw.close();

我将相应的page的url和text内容写入到特定的path下。这样能够方便測试,如上代码段调用的静态方法类例如以下:

package org.apache.nutch.parse.html;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import de.l3s.boilerpipe.BoilerpipeExtractor;
import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.document.TextDocument;
import de.l3s.boilerpipe.extractors.CommonExtractors;
import de.l3s.boilerpipe.sax.BoilerpipeSAXInput; public class BoilerpipeUtils {
public static String getMainbodyTextByBoilerpipe(InputSource is) throws BoilerpipeProcessingException, SAXException{
final TextDocument doc = new BoilerpipeSAXInput(is).getTextDocument();
final BoilerpipeExtractor extractor = CommonExtractors.ARTICLE_EXTRACTOR;
extractor.process(doc);
if(doc.getContent() != null && !doc.getContent().equals(""))
return doc.getContent();
else
return "";
}
}

因为用到了开源的工具boilerpipe。因此须要将相关的jar包放入到插件文件夹下的lib文件夹中。同一时候相应的plugin.xml配置中runtime段例如以下:

<runtime>
<library name="parse-html.jar">
<export name="*"/>
</library>
<library name="tagsoup-1.2.1.jar"/>
<library name="boilerpipe-1.2.0.jar">
</library>
<library name="nekohtml-1.9.13.jar">
</library>
<library name="xerces-2.9.1.jar">
</library>
</runtime>

至此就完毕了插件的功能。在eclipse下执行build project后执行如上的crawl命令,就可以得到自己想要的正文部分的parse_text数据了。假设在cwgwin下执行crawl命令,还会报NoClassDefFound的runtimeException,未指定jar包,上述三个jar包入runtime/local/lib文件夹可以。

然而boilerpipe该文本提取有改进的余地,不理想;进一步定制,也可以用于提取特定网站text信息。

Nutch 二次开发parse纸的更多相关文章

  1. nutch二次开发环境搭建

    开发环境: ubuntu14.04 + jdk1.7 + eclispe +nutch1.7 1:解压下好nutch1.7 src 源码(wget http://archive.apache.org/ ...

  2. nutch 二次开发

    /*深度控制*/ 深度控制:nutch是广域网的深度遍历,我们需要的是垂直采集(即只采集某一个栏目),举例,索引页总计20页,如果只有下一页,则深度为20,如果是1 2 3 4 5……20则深度为2即 ...

  3. Nutch 二次开发之parse正文内容

    关于nutch的基础知识能够參考lemo的专栏 nutch支持二次开发,为了满足搜索的准确率的问题,考虑只将网页正文的内容提取出来作为索引的内容,相应的是parse_text的数据.我使用的事nutc ...

  4. TFS二次开发系列:七、TFS二次开发的数据统计以PBI、Bug、Sprint等为例(一)

    在TFS二次开发中,我们可能会根据某一些情况对各个项目的PBI.BUG等工作项进行统计.在本文中将大略讲解如果进行这些数据统计. 一:连接TFS服务器,并且得到之后需要使用到的类方法. /// < ...

  5. JMeter二次开发(2)-编写 JSON Assertion 插件

    本篇文章主要介绍如何对JMeter进行二次开发,添加自己所需的功能.这里以Json验证为例进行说明.在web接口测试过程中,JSON的应用已经非常普遍,但原声的JMeter并没有提供Json及Json ...

  6. TFS二次开发的数据统计以PBI、Bug、Sprint等为例(一)

    TFS二次开发的数据统计以PBI.Bug.Sprint等为例(一) 在TFS二次开发中,我们可能会根据某一些情况对各个项目的PBI.BUG等工作项进行统计.在本文中将大略讲解如果进行这些数据统计. 一 ...

  7. (5)微信二次开发 之 XML格式数据解析

    1.首先理解一下html html的全名是:HyperText Transfer markup language 超级文本标记语言,html本质上是一门标记(符合)语言,在html里,这些标记是事先定 ...

  8. jeecms系统使用介绍——通过二次开发实现对word、pdf、txt等上传附件的全文检索

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76912307 本文出自[我是干勾鱼的博客] 之前在文章<基于Java的门户 ...

  9. C# 超级狗 二次开发 读写数据 激活验证 存储数据库连接字符串

    本文主要讲解如果使用C#语言来对超级狗进行二次开发,如果仅仅是做个激活的功能,可以参照另一篇博客,地址:http://www.cnblogs.com/dathlin/p/8487842.html 如果 ...

随机推荐

  1. Copy xml 文件

    public static void copyFailFile(String bugID) throws Exception { File file = new File(".") ...

  2. 4.windows和Linux下创建oracleusername表空间,表,插入数据,用户管理表等操作

    进入超级管理员,运行下面命令 Window下创建数据库.表空间,用户,插入数据等操作 -- 01 创建表空间 -- 注意表空间的路径 依据实际安装环境进行调整 CREATE TABLESPACE ts ...

  3. OpenStack25

    OpenStack(25) API 前端服务 每个 OpenStack 组件可能包含若干子服务,其中必定有一个 API 服务负责接收客户请求. 以 Nova 为例,nova-api 作为 Nova 组 ...

  4. 解决com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap

    com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap. 可能存在3种情况: 1.在x ...

  5. Android 启动过程的底层实现

    转载请标明出处:  http://blog.csdn.net/yujun411522/article/details/46367787 本文出自:[yujun411522的博客] 3.1 androi ...

  6. IOC框架之一Autofac

    .NET领域最为流行的IOC框架之一Autofac 一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程序也 ...

  7. JsonCpp Documentation

    JsonCpp - JSON data format manipulation library JsonCpp Documentation 0.6.0-rc2 Introduction JSON (J ...

  8. Nginx使用ngx_zeromq模块返回502错误的解决方法

    /*********************************************************************  * Author  : Samson  * Date   ...

  9. 一些周期性GC的理由为何

    1.供tomcat:防止内存泄漏监听器 JreMemoryLeakPreventionListener在上班,每隔一小时默认触发一次System.gc Class clazz = Class.forN ...

  10. vmware无法链接U盘:vm-->removeable devices.

    vmware无法链接U盘:vm-->removeable devices.