2013-01-14 29 views
2

我是Jsoup的新手,在搜索很長時間後找不到解決方案。 我有一張表,其中tr在末尾有一個帶有空格的類名。Jsoup:在末尾選擇一個類名含有空格的行

<table class="table_one"> 
<tr class="no_background "> 
<td> 
<b> Text comes here</b> 
</td> 
<td> and here... </td> 
</tr></table> 

現在,我想訪問該文本。當我說

Select("table[class=tag_std] tr[class=bgnd_1 ]") 

它返回empty列表。我如何獲得價值爲

"Text comes here and here...". 

謝謝。

+0

你必須只* *,並在後面的空格中選擇它?如果你只想使用'tr.no_background',不管空白如何,都可以使用'tr.no_background'。 – BoltClock

+0

@BoltClock嗨,是的。我需要空白。如果是說 tr.no_background 我得到一個空列表作爲「no_background」不匹配「no_background」(以結束空白) –

回答

2

我認爲你需要把你的標籤放在標籤內而不是。

<table class="table_one"> 
<tr class="no_background "> 
    <td> 
     <b> Text comes here</b> 
    </td> 
</tr> 
</table> 

我認爲你需要這個,根據你的確切情況。這裏有一個簡單的例子供你測試。

public static void main(String[] args) { 
    File input = new File("/Users/hugo/Desktop/test.html"); 
    Document doc = null; 
    try { 
     doc = Jsoup.parse(input, "UTF-8", "http://example.com/"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Elements links = doc.select("table.table_one tr.no_background td"); 
    for (Element element : links) { 
     System.out.println(element.text()); 
    } 
} 

輸出:

Text comes here 
and here. 

..

+0

嗨,請考慮編輯的職位。這是我確切的情況。而且,我還得到第二個​​。 –

+0

嗨,謝謝。有用 :) –

相關問題