使用Java中的XPath来提取HTML中的title的值,可以使用以下代码:

import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

public class XPathExample {
    public static void main(String[] args) throws Exception {
        // HTML内容
        String html = "<html><head><title>Example Title</title></head><body><h1>Hello World</h1></body></html>";

        // 创建XPath工厂
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        // 将HTML内容转换为可处理的XML格式
        InputSource inputSource = new InputSource(new StringReader(html));

        // 编译XPath表达式
        XPathExpression expr = xpath.compile("//title/text()");

        // 执行XPath表达式并获取结果
        Object result = expr.evaluate(inputSource, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        // 提取title的值
        String title = nodes.item(0).getNodeValue();
        System.out.println("Title: " + title);
    }
}

运行以上代码,将会输出:

Title: Example Title

这样就成功提取到了HTML中的title的值

java用xpath提取html中的title的值

原文地址: https://www.cveoy.top/t/topic/ijOC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录