Does jsoup support xpath?

后端 未结 3 1643
迷失自我
迷失自我 2020-12-08 19:24

There\'s some work in progress related to adding xpath support to jsoup https://github.com/jhy/jsoup/pull/80.

  • Is it working?
  • How can I use it?
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 19:55

    JSoup doesn't support XPath yet, but you may try XSoup - "Jsoup with XPath".

    Here's an example quoted from the projects Github site (link):

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

    There you'll also find a list of features and expressions of XPath that are supported by XSoup.

提交回复
热议问题