2012-04-13 38 views
0

我目前正在Jsoup上工作。我已經得到一個元素content看起來像如何刪除Jsoup中的部分網頁?

<p>123</p> 
<p>456</p> 
<p>789</p> 
<p>abc</p> 
<p>efg</p> 
.... 

有幾行EFG行之後,但我要刪除的EFG行之後的所有行,我希望的結果是一個元素(不是元素)

我嘗試了多種方法,如

content.children().removeAll(content.getElementsByIndexGreaterThan(content.children().indexOf(content.select("p:contains(efg)")))); 

content.getElementsByIndexGreaterThan(content.select("p:contains(efg)")).remove(); 

不幸的是,他們都沒有工作。有沒有人有更好的解決方案呢?感謝您閱讀這篇文章。

回答

1
<div> 
<p>123</p> 
<p>456</p> 
<p>789</p> 
<p>abc</p> 
<p>efg</p> 
<p>111</p> 
<p>222</p> 
<p>333</p> 
<p>444</p> 
</div> 

public static void main(String[] args) throws Exception { 
    String html = new String(Files.readAllBytes(Paths.get("input.html"))); 
    Document doc = Jsoup.parse(html); 
    Element content = doc.select("div").first(); 

    Element lastValidElement = content.select("p:contains(efg)").first(); 
    int lastValidElementIndex = content.children().indexOf(lastValidElement); 
    content.getElementsByIndexGreaterThan(lastValidElementIndex).remove(); 
    System.out.println(content); 
} 

<div> 
<p>123</p> 
<p>456</p> 
<p>789</p> 
<p>abc</p> 
<p>efg</p>  
</div>