import net.htmlparser.jericho.*; public class HTMLParserExample { public static void main(String[] args) throws Exception { String html = "<html><body><div><h1>Title</h1><p>Paragraph</p></div></body></html>"; Source source = new Source(html); Element titleElement = source.getFirstElementByXPath("//h1"); if (titleElement != null) { String title = titleElement.getTextExtractor().toString(); System.out.println("Title: " + title); } Element paragraphElement = source.getFirstElementByXPath("//p"); if (paragraphElement != null) { String paragraph = paragraphElement.getTextExtractor().toString(); System.out.println("Paragraph: " + paragraph); } } }


上一篇:
下一篇:
切换中文