2011-08-16 56 views
29

有一些工作正在進行中,將xpath支持添加到jsoup https://github.com/jhy/jsoup/pull/80jsoup是否支持xpath?

  • 它工作嗎?
  • 我該如何使用它?
+0

目前的信息在這個題目勿庸置疑那裏:https://stackoverflow.com/questions/11816878/jsoup- css-selector-code-xpath-code-included https://stackoverflow.com/questions/16335820/convert-xpath-to-jsoup-query https://stackoverflow.com/questions/11791596/how-to-get- https://groups.google.com/forum/?fromgroups#!topic/jsoup/lj4_-EJwH1Q的絕對路徑元素 –

回答

11

JSoup不支持的XPath,但你可以嘗試XSoup - 「Jsoup使用XPath」

下面是從項目Github上的網站(link)引用的例子:

@Test 
public void testSelect() { 

    String html = "<html><div><a href='https://github.com'>github.com</a></div>" + 
      "<table><tr><td>a</td><td>b</td></tr></table></html>"; 

    Document document = Jsoup.parse(html); 

    String result = Xsoup.compile("//a/@href").evaluate(document).get(); 
    Assert.assertEquals("https://github.com", result); 

    List<String> list = Xsoup.compile("//tr/td/text()").evaluate(document).list(); 
    Assert.assertEquals("a", list.get(0)); 
    Assert.assertEquals("b", list.get(1)); 
} 

在那裏,你還可以找到的由XSoup支持的功能和XPath表達式的列表。

1

還沒有,但JsoupXpath具有使it.For例如項目,

String xpath="//div[@id='post_list']/div[./div/div/span[@class='article_view']/a/num()>1000]/div/h3/allText()"; 
String doc = "..."; 
JXDocument jxDocument = new JXDocument(doc); 
List<Object> rs = jxDocument.sel(xpath); 
for (Object o:rs){ 
    if (o instanceof Element){ 
     int index = ((Element) o).siblingIndex(); 
     System.out.println(index); 
    } 
    System.out.println(o.toString()); 
}