2014-09-02 110 views
0

我想解析HTML文本以獲取關鍵字後的特定值。以下代碼爲例:如何使用JSoup獲取特定值?

<table> 

    <tr> 
     <td class="odd">TW-Central</td> 
     <td class="odd">$3.8600</td> 
     <td class="odd">$3.8600</td> 
     <td class="odd">$3.8600</td> 
     <td class="odd red">-0.0168</td> 
     <td class="odd right">42,500</td> 
     <td class="odd right">7</td> 
    </tr> 



    <tr> 
     <td class="even">Waha</td> 
     <td class="even">$3.9600</td> 
     <td class="even">$3.8800</td> 
     <td class="even">$3.9196</td> 
     <td class="even red">-0.0436</td> 
     <td class="even right">69,500</td> 
     <td class="even right">17</td> 
    </tr> 



    <tr> 
     <td class="odd">White River Hub</td> 
     <td class="odd">$3.8200</td> 
     <td class="odd">$3.7975</td> 
     <td class="odd">$3.8088</td> 
     <td class="odd red">-0.0184</td> 
     <td class="odd right">81,200</td> 
     <td class="odd right">13</td> 
    </tr> 

</table> 

在找到關鍵字Waha後,我將如何獲得它下面的價格並返回它? 任何幫助將非常感激。我還使用STS在Java中編寫了這個代碼,如果JSoup不是最好的實現方法,那麼使用什麼建議也將非常感謝!謝謝!

回答

0

如果表格不會改變它的位置,只需獲取所有的td元素,然後使用get(index)方法選擇一個你想要的。

StringBuilder html = new StringBuilder(); 
    html.append(" <table>"); 
    html.append(" <tr>"); 
    html.append("  <td class=\"even\">Waha</td>"); 
    html.append("  <td class=\"even\">$3.9600</td>"); 
    html.append("  <td class=\"even\">$3.8800</td>"); 
    html.append(" </tr>"); 
    html.append(" </table>"); 

    Document document = Jsoup.parse(html.toString()); 
    Elements tdElements = document.select("td"); 
    String waha = tdElements.get(0).text(); 
    String firstPrice = tdElements.get(1).text(); 
    String secondPrice = tdElements.get(2).text(); 

    System.out.println("The first td content is: " + waha); 
    System.out.println("The second td content (firstPrice) is: " + firstPrice); 
    System.out.println("The third td content (secondPrice) is: " + secondPrice); 

更新:

動態地選擇使用下面的代碼:

@Test 
public void testJSOUP() { 
    StringBuilder html = new StringBuilder(); 
    html.append(" <table>"); 
    html.append(" <tr>"); 
    html.append("  <td class=\"odd\">TW-Central</td>"); 
    html.append("  <td class=\"odd\">$3.9600</td>"); 
    html.append("  <td class=\"odd\">$3.8800</td>"); 
    html.append(" </tr>"); 
    html.append(" <tr>"); 
    html.append("  <td class=\"even\">Waha Row</td>"); 
    html.append("  <td class=\"even\">$4.9600</td>"); 
    html.append("  <td class=\"even\">$5.8800</td>"); 
    html.append(" </tr>"); 
    html.append(" <tr>"); 
    html.append("  <td class=\"odd\">White River Hub</</td>"); 
    html.append("  <td class=\"odd\">$4.9600</td>"); 
    html.append("  <td class=\"odd\">$5.8800</td>"); 
    html.append(" </tr>"); 
    html.append(" </table>"); 

    Document document = Jsoup.parse(html.toString()); 
    Elements trElements = document.select("tr"); 
    for (Element tableRows : trElements) { 
     Elements tdElements = tableRows.select("td"); 
     String articleName = tdElements.get(0).text(); 
     String firstPrice = tdElements.get(1).text(); 
     String secondPrice = tdElements.get(2).text(); 

     System.out.println("The article: " + articleName + "has price one:" + firstPrice + " and price two:" + secondPrice); 
    } 
} 

這將產生下面的輸出

文章:TW-Centralhas價格一個:$ 3.9600和價格二:3.8800美元
文章:娃哈羅哈斯價格一:4.9600美元和公關冰之二:$ 5.8800
文章:白河Hubhas一個價格:$ 4.9600及價格二:$ 5.8800

+0

重要的是,HTML代碼需要有效。在你的例子中,它缺少表格標籤 – sandrozbinden 2014-09-02 20:00:27

+0

sandrozbiden Ok真棒這是有道理的,謝謝!問題是,如果表格格式不會改變,並且名稱「Waha」不會改變,那麼只有它的值。此代碼是否仍然有效? 例如,如果價格每天都會更新,我如何獲得新的和未知的價值? – Cris 2014-09-02 20:11:57

+0

如果表格不會改變,那麼您可以始終使用命令tdElements.get(1).text()獲取第一個價格。但是,當然,您需要始終解析完整的html Jsoup.parse(html.toString()),然後在想要查看更改時選擇td。當你的html稍微大一些(裏面有其他表格)時,我們需要找到一個更好的過濾器。 – sandrozbinden 2014-09-02 20:16:23