2012-08-02 67 views
0

這這裏代碼:Jsoup選擇 「不是」

Document doc = Jsoup.connect("http://wikitravel.org/en/San_Francisco").get(); 
System.out.println(doc.select("h2:contains(Get around) ~ *:not(h2:contains(See) ~ *)")); 

輸出http://pastebin.com/gkcCfr1F。是否有一個選擇器使「不」選擇器包含在內?現在它將刪除「see」後面的所有內容,當我想刪除id =「see」以及其他所有內容的最後一個h2標籤時,因爲我試圖解析wiki的各個部分。

,我想獲得的最終輸出是:http://pastebin.com/ntpVrgui

+1

可以手動添加樣本輸出(C/p html你想獲得)這有點難以理解,沒有輸出的例子(對我來說) – ant 2012-08-02 23:28:56

+0

編輯的問題:) – 2012-08-02 23:32:04

回答

0

我會做這樣的事情:

獲取內容的div:

StringBuilder sb = new StringBuilder(); 
    boolean start = false; 
    Document doc = Jsoup.connect("http://wikitravel.org/en/San_Francisco").get(); 
      Elements content = doc.select("#content"); 
      for (Element element : content) { 
       /*Pseudo code 
        if element is h3 and it contains span with id Navigating and if start is 
false append it to stringbuilder, set start to true, else append everything in between until you reach h2 with span id See 
        */     
    } 
+0

if(element.tagName()。equals(「h3」)) 我什至不能得到條件「如果元素是h3「工作。有任何想法嗎?另外,我注意到content.size()返回1,這意味着只有一個元素... – 2012-08-03 06:28:40

+0

使用doc.select(「#content」)只返回一個元素,所以我切換到doc.getAllElements() 。我能夠通過if(element.tagName()。equals(「h3」))得到第一個條件,但我不知道id爲「navigating」的跨度。我一直在閱讀api幾個小時。 – 2012-08-03 06:35:59

+0

我得到它的工作http://pastebin.com/GQyDyxPT ...但現在一個錯誤仍然 最後,有一些垃圾html看起來像這樣,出現在html生成的http://底部pastebin.com/jwnaz4R9 – 2012-08-04 03:15:23