dev_marshell08
dev_marshell08

Reputation: 1101

unable to extract contents from html using jsoup in java?

I am trying to use jsoup to extract the contents from the below HTML code in the following < td > tags which has class css-sched-table-title and css-sched-waypoints. But i am not able to understand whats going wrong can someone help ?

Document doc = Jsoup.parse("somelink.html");
    Elements row = doc.select(".css-sched-table-title td");
    Iterator<Element> iterator = row.listIterator();
    while(iterator.hasNext())
    {
       Element element = iterator.next();
        String value = element.text();
        System.out.println("value : " + value);
    }

.

  <tr>
        <td ALIGN="CENTER" COLSPAN="16"  CLASS="css-sched-table-title"><b>Saturday - </b><b>Afternoon</b></td>
    </tr>
    <tr VALIGN="BOTTOM">
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Townline and Southern</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Clearbrook and Blueridge</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Clearbrook and South Fraser</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Ar. Bourquin Exchange</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Lv. Bourquin Exchange</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Downtown Abbotsford</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">McMillan and Old Yale</TD>
        <TD>&nbsp;</TD>
        <TD ALIGN="CENTER" WIDTH="100" CLASS="css-sched-waypoints">Sandy Hill and Old Clayburn</TD>
    </tr>

Upvotes: 1

Views: 218

Answers (1)

StoopidDonut
StoopidDonut

Reputation: 8617

There is a single td tag with css-sched-table-title but a list with css-sched-waypoints.

Also, aligning to the correct syntax it should be Elements row = doc.select("td.css-sched-waypoints");, refer to here.

Note: html file is used as is is invalid and jsoup would not interpret it as valid table html contents. I had to enclose the content above within <table></table> tags.

When I try the below code with your html file:

Elements row = doc.select("td.css-sched-waypoints");
    Element title = doc.select("td.css-sched-table-title").first();

    System.out.println(title.text());
    Iterator<Element> iterator = row.listIterator();
    while (iterator.hasNext()) {
        Element element = iterator.next();
        String id = element.attr("id");
        String classes = element.attr("class");
        String value = element.text();
        System.out.println("Id : " + id + ", classes : " + classes
                + ", value : " + value);
    }

I get,

Saturday - Afternoon
Id : , classes : css-sched-waypoints, value : Townline and Southern
Id : , classes : css-sched-waypoints, value : Clearbrook and Blueridge
Id : , classes : css-sched-waypoints, value : Clearbrook and South Fraser
Id : , classes : css-sched-waypoints, value : Ar. Bourquin Exchange
Id : , classes : css-sched-waypoints, value : Lv. Bourquin Exchange
Id : , classes : css-sched-waypoints, value : Downtown Abbotsford
Id : , classes : css-sched-waypoints, value : McMillan and Old Yale
Id : , classes : css-sched-waypoints, value : Sandy Hill and Old Clayburn

Upvotes: 1

Related Questions