Jeff
Jeff

Reputation: 97

Jsoup: How get all the href associated with a specific class

So I have this code the gets the href from a class. But I want to make it print all the hrefs that have the same class.

This is the part of my code that gets the href from that class.

Document doc = Jsoup.parse(file, null);
String links = doc.select("a.group-link-for-image").first().attr("abs:href");
System.out.println(links);

This is the output:

http://dogtime.com/dog-breeds/affenpinscher

I want to be able to print all the hrefs not just one that has the same class group-link-for-image.

Thank you in advance.

Upvotes: 1

Views: 310

Answers (1)

pikachu0
pikachu0

Reputation: 812

// First, find all elements that fit your criteria
Elements links = doc.select("a.group-link-for-image");
// Then, walk through the found elements and collect the information you want
for (Element link : links) {
    System.out.println(link.attr("abs:href"));
}

The code snippet uses jsoup 1.11.2 API.

Upvotes: 1

Related Questions