如何避免Jsoup解析中的周围html头标记

时间:2022-10-29 17:02:57

Using Jsoup i try to parse the given html content. After Jsoup.parse() the html output append html, head and body tag to the input. I just want to ignore these.

使用Jsoup我尝试解析给定的html内容。在Jsoup.parse()之后,html输出将html,head和body标签附加到输入。我只是想忽略这些。

Sample Input:

<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

Java code:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HTMLParse {

    public static void main(String args[]) throws IOException {
        try{
            File input = new File("/ab.html");
            String html = FileUtils.readFileToString(input, null);

            Document doc = Jsoup.parseBodyFragment(html);
            doc.outputSettings().prettyPrint(false);
            System.out.println(doc.html());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

Actual output:

<html><head></head><body><p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
    </body></html>

Expected Output:

<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

Please help.

3 个解决方案

#1


15  

The cause:

parseBodyFragment() as well as all other parse()-methods use a HTML parser by default. And those add always the HTML-Shell (<html>…</html>, <head>…</head> etc.).

parseBodyFragment()以及所有其他parse()方法默认使用HTML解析器。那些总是添加HTML-Shell( ... , ... 等)。

The Solution:

Just don't use a HTML-parser, use a XML-parser instead ;-)

只是不要使用HTML解析器,而是使用XML解析器;-)

Document doc = Jsoup.parse(html, "", Parser.xmlParser());

Replace that single line and your problem is solved.

替换该单行,您的问题就解决了。

Example:

final String html = "<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>";

Document docHtml = Jsoup.parse(html);
Document docXml = Jsoup.parse(html, "", Parser.xmlParser());

System.out.println("******* HTML *******\n" + docHtml);
System.out.println();
System.out.println("*******  XML *******\n" + docXml);

Output:

******* HTML *******
<html>
 <head></head>
 <body>
  <p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
 </body>
</html>

*******  XML *******
<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

#2


5  

To get the expected output it would actually be:

要获得预期的输出,它实际上是:

final String html = "<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>";
Document doc = Jsoup.parseBodyFragment(html);
doc.outputSettings().prettyPrint(false);

System.out.println(doc.body().html());

#3


2  

You can try using the XML parser, but this doesn't always work because HTML is not always XML; it often has unterminated tags like <img> and <br>. It's better to stick with the HTML parser. You can rely on there being <html>, <head>, and <body> tags and they are easy to discard. Just get your fragment of HTML by selecting the body tag and ask for its HTML.

您可以尝试使用XML解析器,但这并不总是有效,因为HTML并不总是XML;它通常有未终止的标签,如如何避免Jsoup解析中的周围html头标记
。坚持使用HTML解析器更好。您可以依赖,和标签,它们很容易丢弃。只需通过选择body标签获取HTML片段并询问其HTML。

Document doc = Jsoup.parseBodyFragment(html);
        doc.outputSettings().prettyPrint(false);
        System.out.println(doc.select("body").html());

#1


15  

The cause:

parseBodyFragment() as well as all other parse()-methods use a HTML parser by default. And those add always the HTML-Shell (<html>…</html>, <head>…</head> etc.).

parseBodyFragment()以及所有其他parse()方法默认使用HTML解析器。那些总是添加HTML-Shell( ... , ... 等)。

The Solution:

Just don't use a HTML-parser, use a XML-parser instead ;-)

只是不要使用HTML解析器,而是使用XML解析器;-)

Document doc = Jsoup.parse(html, "", Parser.xmlParser());

Replace that single line and your problem is solved.

替换该单行,您的问题就解决了。

Example:

final String html = "<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>";

Document docHtml = Jsoup.parse(html);
Document docXml = Jsoup.parse(html, "", Parser.xmlParser());

System.out.println("******* HTML *******\n" + docHtml);
System.out.println();
System.out.println("*******  XML *******\n" + docXml);

Output:

******* HTML *******
<html>
 <head></head>
 <body>
  <p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
 </body>
</html>

*******  XML *******
<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

#2


5  

To get the expected output it would actually be:

要获得预期的输出,它实际上是:

final String html = "<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>";
Document doc = Jsoup.parseBodyFragment(html);
doc.outputSettings().prettyPrint(false);

System.out.println(doc.body().html());

#3


2  

You can try using the XML parser, but this doesn't always work because HTML is not always XML; it often has unterminated tags like <img> and <br>. It's better to stick with the HTML parser. You can rely on there being <html>, <head>, and <body> tags and they are easy to discard. Just get your fragment of HTML by selecting the body tag and ask for its HTML.

您可以尝试使用XML解析器,但这并不总是有效,因为HTML并不总是XML;它通常有未终止的标签,如如何避免Jsoup解析中的周围html头标记
。坚持使用HTML解析器更好。您可以依赖,和标签,它们很容易丢弃。只需通过选择body标签获取HTML片段并询问其HTML。

Document doc = Jsoup.parseBodyFragment(html);
        doc.outputSettings().prettyPrint(false);
        System.out.println(doc.select("body").html());