Brandon
Brandon

Reputation: 89

Iterating through elements in jsoup and parsing href

I was having trouble getting just the href from a rows of table data. Although I was able to get it working, I am wondering if anyone has an explanation for why my code here works.

for (Element element : result.select("tr")) {
    if (element.select("tr.header.left").isEmpty()) {

        Elements tds = element.select("td");

        //The line below is what I don't understand
        String link = tds.get(0).getElementsByAttribute("href").first().attr("href");

        String position = tds.get(1).text();
     }
}

The line that I was using before, that did not work is below:

String link = tds.get(0).attr("href");

Why does this line return an empty string? I'm assuming it has to do with how I am iterating through the elements as I've selected by "tr". However, I'm not familiar with how Elements vs Element are structured.

Thanks for your help!

Upvotes: 1

Views: 546

Answers (1)

Andrew Regan
Andrew Regan

Reputation: 5113

Elements is simply an ArrayList<Element>

The reason you're having to write that extra code is because <td> doesn't have an href attribute, so tds.get(0).attr("href"); won't work. You're presumably trying to capture the href from an <a> within the cell. The longer, working code is saying:

For the first cell in the row, get the first element with an @href attribute (i.e. a link), and get its @href attribute

Try the following example (with example document) to show how to access the child links more clearly:

Element result = Jsoup.parse("<html><body><table><tr><td><a href=\"http://a.com\" /</td><td>Label1</td></tr><tr><td><a href=\"http://b.com\" /></td><td>Label2</td></tr></table></body></html>");

for (Element element : result.select("tr")) {
    if (element.select("tr.header.left").isEmpty()) {

        Elements tds = element.select("td");

        String link = tds.get(0).getElementsByTag("a").attr("href");
        String position = tds.get(1).text();

        System.out.println(link + ", " + position);
    }
}

Upvotes: 2

Related Questions