Java 使用 XPath 提取 HTML 中的 title 标签值
使用 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'的值。
原文地址: https://www.cveoy.top/t/topic/p1WC 著作权归作者所有。请勿转载和采集!