本文介绍了Apache Tika如何从页眉和页脚内容中提取html正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找除了页眉和页脚之外的HTML的全部内容,但是我收到了异常

I am looking to extract entire body content of html except header and footer, however I am getting exception

下面是我创建的代码,如前所述

Below is my code that i have created as mentioned at

import org.apache.tika.exception.TikaException;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tika.sax.ToHTMLContentHandler;
import org.apache.tika.sax.ToXMLContentHandler;
import org.apache.tika.sax.XHTMLContentHandler;
import org.apache.tika.sax.xpath.Matcher;
import org.apache.tika.sax.xpath.MatchingContentHandler;
import org.apache.tika.sax.xpath.XPathParser;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.tika.metadata.Metadata;

import java.io.File;
//import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class NewtikaXpath {
    public static void main(String args[]) throws IOException, SAXException, TikaException {
        XPathParser xhtmlParser = new XPathParser("xhtml", XHTMLContentHandler.XHTML);
        Matcher divContentMatcher = xhtmlParser.parse("/xhtml:html/xhtml:body/xhtml:table/descendant::node()");
        ContentHandler xhandler = new MatchingContentHandler(new ToXMLContentHandler(), divContentMatcher);
        AutoDetectParser parser = new AutoDetectParser();
        //ToHTMLContentHandler htmlhandler = new ToHTMLContentHandler();
        //ContentHandler textHandler = new BodyContentHandler();
        Metadata xmetadata = new Metadata();
        try  (InputStream stream = TikaInputStream.get(new File("///httpmoneycnncomnewsworldiidHBNQL1.html"))){
            parser.parse(stream, xhandler, xmetadata);
            System.out.println("text:\n" + xhandler.toString());
        }
    }

}

例外我得到的是

the exception I am getting is

Exception in thread "main" org.xml.sax.SAXException: Namespace http://www.w3.org/1999/xhtml not declared
at org.apache.tika.sax.ToXMLContentHandler$ElementInfo.getPrefix(ToXMLContentHandler.java:62)
at org.apache.tika.sax.ToXMLContentHandler$ElementInfo.getQName(ToXMLContentHandler.java:68)
at org.apache.tika.sax.ToXMLContentHandler.startElement(ToXMLContentHandler.java:148)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.xpath.MatchingContentHandler.startElement(MatchingContentHandler.java:60)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.SecureContentHandler.startElement(SecureContentHandler.java:250)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.sax.SafeContentHandler.startElement(SafeContentHandler.java:264)
at org.apache.tika.sax.XHTMLContentHandler.startElement(XHTMLContentHandler.java:255)
at org.apache.tika.sax.XHTMLContentHandler.startElement(XHTMLContentHandler.java:285)
at org.apache.tika.parser.html.HtmlHandler.startElementWithSafeAttributes(HtmlHandler.java:171)
at org.apache.tika.parser.html.HtmlHandler.startElement(HtmlHandler.java:133)
at org.apache.tika.sax.ContentHandlerDecorator.startElement(ContentHandlerDecorator.java:126)
at org.apache.tika.parser.html.XHTMLDowngradeHandler.startElement(XHTMLDowngradeHandler.java:60)
at org.ccil.cowan.tagsoup.Parser.push(Parser.java:794)
at org.ccil.cowan.tagsoup.Parser.rectify(Parser.java:1061)
at org.ccil.cowan.tagsoup.Parser.stagc(Parser.java:1016)
at org.ccil.cowan.tagsoup.HTMLScanner.scan(HTMLScanner.java:567)
at org.ccil.cowan.tagsoup.Parser.parse(Parser.java:449)
at org.apache.tika.parser.html.HtmlParser.parse(HtmlParser.java:122)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:120)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:136)
at com.fractal.NewtikaXpath.main(NewtikaXpath.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

虽然我明白按照我们不应该包装内容处理程序,我没有看到任何解决此问题的替代方法,因为简单的bodycontenthandler没有帮助,我验证了很多类似于此的stackoverflow案例,但我找不到任何解决方案。任何建议或解决方案或指针非常赞赏。

While I do understand that as per TIKA-1215 we should not wrap content handlers , I dont see any alternative approach to resolving this issue, as the simple bodycontenthandler is not helping, I verified a lot of stackoverflow cases similar to this but I couldn't find a solution any where. Any advice or solution or pointer is much appreciated.

推荐答案

Foudn解决方案根据研究检测,这与apache tika集成在一起,可以用下面的java代码运行。

Foudn a solution at based on research boilerpipe detection and this is integrated with apache tika and can be run with the below java code.

import org.apache.tika.exception.TikaException;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.html.BoilerpipeContentHandler;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.tika.metadata.Metadata;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class NewtikaXpath {
    public static void main(String args[]) throws IOException, SAXException, TikaException {
        AutoDetectParser parser = new AutoDetectParser();
        ContentHandler textHandler = new BodyContentHandler();
        Metadata xmetadata = new Metadata();
        try  (InputStream stream = TikaInputStream.get(new URL("your favourite url"))){
            parser.parse(stream, new BoilerpipeContentHandler(textHandler), xmetadata);
            System.out.println("text:\n" + textHandler.toString());
        }
    }

}

你可以有一个简单的演示锅炉管道检测和更多信息也可以

You can have a simple demo of boilerpipe detection at.. and more information can be also available at..

这篇关于Apache Tika如何从页眉和页脚内容中提取html正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:48