Java XPath 使用(2023/08/29)

Java XPath 使用(2023/08/29)

文章目录

  • [Java XPath 使用(2023/08/29)](#Java XPath 使用(2023/08/29))
    • [1. 前言](#1. 前言)
    • [2. 技术选型](#2. 技术选型)
    • [3. 技术实现](#3. 技术实现)

1. 前言

众所周知,Java 语言适合应用于 Web 开发领域,不擅长用来编写爬虫。但在 Web 开发过程中有时又存在爬取数据的需求,此时采用其它语言编写独立爬虫模块的话存在维护不方便的问题,所以此处笔者选择了使用 Java + XPath 实现简单的爬虫功能,如果爬虫需求较多且复杂还是推荐采用其它语言实现独立的爬虫模块。

2. 技术选型

  1. JsoupXpath
    • 优点:使用简单;
    • 缺点:对 XPath 语法的支持有限;
  2. xsoup
    • 优点:使用简单;
    • 缺点:对 XPath 语法的支持有限;
  3. HtmlCleaner
    • 优点:使用简单;
    • 缺点:对 XPath 语法的支持有限;
  4. Java XPath
    • 优点:对 XPath 语法支持全面;
    • 缺点:对 xml 格式要求严格,几乎没有 Html 可以通过解析;
  5. HtmlCleaner + Java XPath
    • 优点:对 XPath 语法支持全面;
    • 缺点:使用相对复杂;

3. 技术实现

http://www.jnswj.net/jsp/sw/jnsw-skhdsq.jsp 网站和 //*[@id="MainTable"]/tbody/tr[position()>=5 and position()<=22]/td[position()=1 or (position()>=9 and position()<=13)] XPath 表达式为例,笔者测试了上述 5 种技术方案,其中只有第 5 种方案通过了测试,其它几种均出现了报错,故此处仅介绍第 5 种方案的实现。

  1. Maven 引入依赖;

    xml 复制代码
    <!-- 获取 HTML 页面内容 -->
    <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-http -->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-http</artifactId>
        <version>5.8.21</version>
    </dependency>
    
    <!-- 解析 HTML -->
    <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlcleaner/htmlcleaner -->
    <dependency>
        <groupId>net.sourceforge.htmlcleaner</groupId>
        <artifactId>htmlcleaner</artifactId>
        <version>2.29</version>
    </dependency>
  2. 获取页面内容,并解析获取结果;

    java 复制代码
    import cn.hutool.http.HttpUtil;
    import org.htmlcleaner.CleanerProperties;
    import org.htmlcleaner.DomSerializer;
    import org.htmlcleaner.HtmlCleaner;
    import org.htmlcleaner.TagNode;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    
    
    /**
     * 测试 HtmlCleaner + Java XPath.
     *
     * @author <a href="mailto:xiaoQQya@126.com">xiaoQQya</a>
     * @since 2023/08/29
     */
    private void test throws ParserConfigurationException, XPathExpressionException {
        // 获取 HTML 页面内容
        String url = "http://www.jnswj.net/jsp/sw/jnsw-skhdsq.jsp";
        String html = HttpUtil.get(url);
    
        // 解析 HTML 页面内容
        HtmlCleaner hc = new HtmlCleaner();
        TagNode tn = hc.clean(html);
        Document document = new DomSerializer(new CleanerProperties()).createDOM(tn);
        
        // 匹配获取需要的数据
        XPath xPath = XPathFactory.newInstance().newXPath();
        String exp = "//*[@id=\"MainTable\"]/tbody/tr[position()>=5 and position()<=22]/td[position()=1 or (position()>=9 and position()<=13)]";
        NodeList nodes = (NodeList) xPath.evaluate(exp, document, XPathConstants.NODESET);
    
        for (int length = nodes.getLength(), i = 0; i < length; i++) {
            Node item = nodes.item(i);
            System.out.println(item.getTextContent());
        }
    }

参考文章:

相关推荐
c***4210几秒前
爬虫基础之爬取某基金网站+数据分析
爬虫·数据挖掘·数据分析
likuolei1 小时前
XML 元素 vs. 属性
xml·java·开发语言
自不量力的A同学1 小时前
Spring Boot 4.0.0 正式发布
java·spring boot·后端
d***29241 小时前
【spring】Spring事件监听器ApplicationListener的使用与源码分析
java·后端·spring
5***b971 小时前
解决报错net.sf.jsqlparser.statement.select.SelectBody
java
q***95221 小时前
Tomcat下载,安装,配置终极版(2024)
java·tomcat
2***d8851 小时前
详解tomcat中的jmx监控
java·tomcat
无敌最俊朗@2 小时前
Qt事件循环队列剖析!!!
java
v***5652 小时前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
python零基础入门小白2 小时前
【万字长文】大模型应用开发:意图路由与查询重写设计模式(从入门到精通)
java·开发语言·设计模式·语言模型·架构·大模型应用开发·大模型学习