Jsoup使用总结

1. 概述

对HTML页面的解析,之前我一般使用HTMLParser,详细见HTMLParser的学习系列 - 学习总结,但是这个项目已经停止更新。现在比较好的解析HTML的控件是Jsoup。本文对Jsoup的用法做个总结 。
Jsoup的主要功能有三部分组成:

  1. 从字符串,网页,本地文件等方式生成Documentn
  2. 在生成Doucment后,根据Dom和css或类似jquery的selector语法获取Element,然后再从Elements中获取节点属性、文本、html
  3. 对Element的进行操作,包括HTML的值、节点内容的值和设置节点属性的值

下方每节对以上三点进行逐一演示。

POM.xml 配置

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.10.2</version>
</dependency>

2. 生成Document

JSOUP通过不同方式生成Document,主要有以下三种:
1. 字符串
2. 网页
3. 本地文件

2.1 从字符串生成Document

关键方法:Jsoup.parse
代码

    /**
     * 将字符串转化为Document
     * 
     * html: https://jsoup.org/cookbook/input/parse-document-from-string
     */
    public void parseDocumentFromString() {
        String html = "<html><head><title>First parse</title></head>"
                + "<body><p>Parsed HTML into a doc.</p></body></html>";
        Document doc = Jsoup.parse(html);
        logger.info("parseDocumentFromString content={}", doc);
    }

输出结果

parseDocumentFromString content=<html>
 <head>
  <title>First parse</title>
 </head>
 <body>
  <p>Parsed HTML into a doc.</p>
 </body>
</html>

2.2 从网页生成Document

关键方法: Jsoup.connect
代码

/**
     * 从网络上加载网页并转化为Document
     * 
     * html: https://jsoup.org/cookbook/input/load-document-from-url
     */
    public void loadDocumentFromURL() {
        Document doc;
        try {
            doc = Jsoup.connect("https://www.baidu.com/").get();
            // 从document中获取title值
            String title = doc.title();
            logger.info("LoadDocumentFromURL title={}", title);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

输出:由于页面太大,这里没有打印页面内容,只是输出页面的title值

LoadDocumentFromURL title=百度一下,你就知道

2.3 从本地文件生成Document

本地测试文件: loadDocumentFromFile.html

<html>
<body>
.. body
</body>
</html>

代码
关键方法: Jsoup.parse

    /**
     * 从本地加载文件并转化为Document
     * 
     * html: https://jsoup.org/cookbook/input/load-document-from-file
     */
    public void loadDocumentFromFile() {
        URL fileUrl = LoadParseDocument.class.getResource("/com/hry/tool/jsoup/doc/loadDocumentFromFile.html");
        File input = new File(fileUrl.getFile());
        try {
            /* The baseUri parameter is used by the parser to resolve relative URLs in the document 
             * before a <base href> element is found. If that's not a concern for you, 
             * you can pass an empty string instead.
             */
            Document doc = Jsoup.parse(input, "UTF-8", "https://www.baidu.com/");
            logger.info("LoadDocumentFromFile content={}", doc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

输出

LoadDocumentFromFile content=<html>
 <head></head>
 
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值