Library to query HTML with XPath in Java?

后端 未结 5 415
失恋的感觉
失恋的感觉 2020-12-03 16:31

Can anyone recommend me a java library to allow me XPath Queries over URLs? I\'ve tried JAXP without success.

Thank you.

5条回答
  •  眼角桃花
    2020-12-03 17:08

    Use Xsoup. According to the docs, it's faster than HtmlCleaner. Example

     @Test
        public void testSelect() {
    
            String html = "" +
                    "
    ab
    "; Document document = Jsoup.parse(html); String result = Xsoup.compile("//a/@href").evaluate(document).get(); Assert.assertEquals("https://github.com", result); List list = Xsoup.compile("//tr/td/text()").evaluate(document).list(); Assert.assertEquals("a", list.get(0)); Assert.assertEquals("b", list.get(1)); }

    Link to Xsoup - https://github.com/code4craft/xsoup

提交回复
热议问题