zoukankan      html  css  js  c++  java
  • Nutch 二次开发parse纸


    大约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() + "
    ");
        	  fw.write("text::" + text + "
    ");
        	  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信息。





     

  • 相关阅读:
    笔记
    BlangenOA项目展示(附源码)
    笔记截至20190406
    ASP.NET MVC 使用过滤器需要注意
    单例模式和HttpContext线程内唯一
    C#线程/进程同步(lock、Mutex、Semaphore)
    Web标准
    JavaScript 放置在文档最后面可以使页面加载速度更快
    GUI 面板实现 (解决了关闭事件)
    GUI 实现多个窗口 (使用封装特性)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4593748.html
Copyright © 2011-2022 走看看