Convert xPath to JSoup query

前端 未结 6 1462
南旧
南旧 2020-12-09 10:29

Does anyone know of an xPath to JSoup convertor? I get the following xPath from Chrome:

 //*[@id=\"docs\"]/div[1]/h4/a

and would like to c

6条回答
  •  爱一瞬间的悲伤
    2020-12-09 10:48

    You don't necessarily need to convert Xpath to JSoup specific selectors.

    Instead you can use XSoup which is based on JSoup and supports Xpath.

    https://github.com/code4craft/xsoup

    Here is an example using XSoup from the docs.

    @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)); }

提交回复
热议问题