2017-04-03 25 views
0

我有以下的html代碼:如何只用JSoup獲取外部div文本?

<div class="description"> 
    <div class='daterange'> 
     Hello 
    <span itemprop='startDate'> 
     June 3, 2011 
    </span> 
    </div> 
    This is some description <i>that</i> I want to fetch 
</div><br/> 

,我想只提取部分:

This is some description <i>that</i> I want to fetch 

如何我jsoup辦呢?

我試過使用String description = doc.select("div.description").text(),但後來我收到了裏面的所有內容。

回答

0

你需要的是創建一個字符串,它將保存html文件的文字。 這是由以下代碼生成的,doc.body()。text()取得的文本沒有所有的html標籤。

`public String getWords(String url) { 
     String text = ""; 
     try { 
      Document doc = Jsoup.connect(url).get(); 
      text = doc.body().text(); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
     return text; 
    } 
` 
+0

嗯,不過我認爲你的做法也將提取'Hello'和'6月3日,2011',我想避免 – user3766930

+0

我想我找到了解決辦法爲您的問題,檢查這個答案:http://stackoverflow.com/questions/16835878/use-jsoup-extract-certain-part-of-text – Flika205

0

試試這個

String description = doc.select("div").remove().first().html(); 
+0

它沒有工作:( – user3766930

+0

它與這個確切的HTML片段。發佈完整的HTML頁面,如果它不太大 – Reimeus