在Java中获取HTML文档中指定标签的内容,可以使用多种库来实现,其中最常用的是Jsoup。Jsoup是一个非常强大且易于使用的库,用于处理HTML数据。以下是如何使用Jsoup来获取HTML文档中指定标签的内容的示例。

1. 添加Jsoup依赖

首先,项目中添加Jsoup的依赖。使用Maven,可以在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.15.3</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
2. 使用Jsoup获取指定标签的内容

以下是一个示例代码,展示了如何使用Jsoup从HTML文档中获取指定标签的内容。

示例HTML文档

假设有以下HTML文档:

<!DOCTYPE html>
<html>
<head>
    <title>Example Page</title>
</head>
<body>
    <h1>Welcome to Example Page</h1>
    <p>This is a paragraph.</p>
    <a href="https://example.com">Link to Example</a>
    <div id="content">
        <p>Some content inside a div.</p>
    </div>
</body>
</html>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
Java代码
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlContentExtractor {

    public static void main(String[] args) {
        // 读取HTML内容
        String htmlContent = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <title>Example Page</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h1>Welcome to Example Page</h1>\n" +
                "    <p>This is a paragraph.</p>\n" +
                "    <a href=\"https://example.com\">Link to Example</a>\n" +
                "    <div id=\"content\">\n" +
                "        <p>Some content inside a div.</p>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";

        // 解析HTML内容
        Document doc = Jsoup.parse(htmlContent);

        // 获取指定标签的内容
        // 获取所有<h1>标签
        Elements h1Elements = doc.select("h1");
        for (Element h1 : h1Elements) {
            System.out.println("h1: " + h1.text());
        }

        // 获取所有<p>标签
        Elements pElements = doc.select("p");
        for (Element p : pElements) {
            System.out.println("p: " + p.text());
        }

        // 获取id为"content"的<div>标签内的所有<p>标签
        Elements contentPElements = doc.select("#content p");
        for (Element p : contentPElements) {
            System.out.println("p in #content: " + p.text());
        }

        // 获取所有<a>标签及其href属性
        Elements aElements = doc.select("a");
        for (Element a : aElements) {
            System.out.println("a: " + a.text() + ", href: " + a.attr("href"));
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
代码解释
  1. 读取HTML内容
  • 将HTML内容存储在一个字符串变量 htmlContent 中。你也可以从文件或网络中读取HTML内容。
  1. 解析HTML内容
  • 使用 Jsoup.parse(htmlContent) 方法将HTML字符串解析为 Document 对象。
  1. 选择指定标签
  • 使用 doc.select("tag") 方法选择指定的标签。例如,doc.select("h1") 选择所有 <h1> 标签。
  • 使用 doc.select("#id tag") 方法选择具有特定ID的元素内的指定标签。例如,doc.select("#content p") 选择ID为 content<div> 元素内的所有 <p> 标签。
  1. 提取文本内容
  • 使用 element.text() 方法提取元素的文本内容。
  • 使用 element.attr("attribute") 方法提取元素的属性值,例如 a.attr("href") 提取 <a> 标签的 href 属性。
运行结果

运行上述代码,输出结果如下:

h1: Welcome to Example Page
p: This is a paragraph.
p in #content: Some content inside a div.
a: Link to Example, href: https://example.com
  • 1.
  • 2.
  • 3.
  • 4.

通过这些步骤,可以使用Jsoup从HTML文档中提取指定标签的内容。