2012-02-07 103 views
3

Apache Tika的GUI实用程序提供了获取给定文档或URL的主要内容(格式文本和结构化文本除外)的选项。我只想知道哪个方法负责提取文档/网址的主要内容。以便我可以将该方法合并到我的程序中。此外,他们是否在从HTML页面提取数据时使用任何启发式算法。因为有时在提取的内容中,我无法看到广告。我们发现BoilerPipeContentHandler负责它。Tika - 从文档检索主要内容

+0

在下面的问题中提供了一个解决方案,使用boilerpipe。 http://stackoverflow.com/questions/42589076/apache-tika-how-to-extract-html-body-with-out-header-and-footer-content – 2017-03-08 04:33:45

回答

7

Tika GUI中的“主要内容”功能是使用BoilerpipeContentHandler类实现的,该类依靠boilerpipe library进行繁重工作。

+0

它只能用于HTML页面或全部。因为从Boilerpipe文档中,我可以看到它主要仅支持HTML页面。 – Allwyn 2012-02-09 04:22:26

+0

也可以帮助告诉我如何控制Tika输出中的空格和换行符。因为tika的输出包含更多的空格和换行符 – Allwyn 2012-02-09 04:32:32

0

我相信这是由BodyContentHandler驱动的,它只提取文档正文的HTML内容。如果需要,这可以另外与其他处理程序组合以仅返回主体的纯文本。

+0

我试过了,但它不能提取主要内容。 – Allwyn 2012-02-08 05:05:01

+0

它适用于所有的单元测试...我建议你看看它是如何使用的,并将其与你的使用进行比较 – Gagravarr 2012-02-08 12:37:20

+0

@Gagravarr“主要内容”是纯文本,带有剥离的样板文件(尝试实验用Tika gui来看看它是什么)。 – 2014-04-27 21:07:26

0
public String[] tika_autoParser() { 
    String[] result = new String[3]; 
    try { 
     InputStream input = new FileInputStream(new File(path)); 
     ContentHandler textHandler = new BodyContentHandler(); 
     Metadata metadata = new Metadata(); 
     AutoDetectParser parser = new AutoDetectParser(); 
     ParseContext context = new ParseContext(); 
     parser.parse(input, textHandler, metadata, context); 
     result[0] = "Title: " + metadata.get(metadata.TITLE); 
     result[1] = "Body: " + textHandler.toString(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } catch (TikaException e) { 
     e.printStackTrace(); 
    } 

    return result; 
}