Keleth
Keleth

Reputation: 23

Selecting certain <tr> tag with JSoup

I am new to JSoup and have been working with it for a few days now with no problems, until I came across this one. I'm trying to get all the <tr> tags from a table where the <tr>s have a child <td> tag with a certain class.

I am trying to retrieve the data from this website, this is what I'm trying:

document.select("#partedenieve tr:has(td.zonas)");

I don't know if it works because the problem here is that if you select just #partedenieve tr it only returns the <tr>s on the thead. I've tried some other queries, but when I finally achieve to get <tr>s from the tbody it won't return all the <tr> tags either.

I don't know if this problem may be related to the rowspan tag present on the <tr>s I want to get... but I've had no luck so far.

Thanks in advance for your replies.

Upvotes: 0

Views: 267

Answers (1)

Alexis Dufrenoy
Alexis Dufrenoy

Reputation: 11946

Try:

Elements elts = document.select("tr > td.class");

This will return all td elements with the given class. So you just have to get each unique parent:

List<Element> list = new ArrayList<Element>();
for (Element elt : elts) {
    if (!list.contains(elt) {
        list.add(elt);
    }
}

Now, your list object contains all of your tr elements.

Upvotes: 1

Related Questions