简体   繁体   中英

How to use JSoup to grab a specific value?

I'm trying to parse html text to grab a specific value after a keyword. For Example on the code below:

<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>

After the keyword Waha is found, how would I be able to grab the price under it and return it? Any help would be very much appreciated. I'm also coding this in Java using STS, if JSoup is not the best to achieve this, advice on what to use would also be much appreciated! Thanks!

If the table is not going to change it's position just get all the td elements and then select with the get(index) method the one you want.

    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);

Update:

To dynamically select use the following code:

@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);
    }
}

This will create the following output

The article: TW-Centralhas price one: $3.9600 and price two: $3.8800
The article: Waha Rowhas price one: $4.9600 and price two: $5.8800
The article: White River Hubhas price one: $4.9600 and price two: $5.8800

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM