Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java jsoup – all links belonging to a specific class – no output, no error message

I’m learning jsoup in Java.

I’d like to print all links belonging to a specific class.

I’m not getting any output when my expected output is my beutiful link : /sport/golf/66227692\.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve looked at Jsoup: How get all the href associated with a specific class, Jsoup – get text from all elements with a particular class under a Specified class, and https://jsoup.org/cookbook/extracting-data/selector-syntax, but I don’t know where I’m going wrong.

Thanks for any help.

import java.io.IOException;
import org.jsoup.Jsoup;  
import org.jsoup.nodes.Document;  
import org.jsoup.nodes.Element;  
import org.jsoup.select.Elements;


public class Anyclass {

    public static void main(String[] args) throws IOException {
    
        String html = "<a href=\"/sport/golf/66227692\" class=\"ssrcss-6m4230-PromoLink e1f5wbog1\"><span role=\"text\"><p class=\"ssrcss-6arcww-PromoHeadline e1f5wbog6\"><span aria-hidden=\"false\">McIlroy's 'perfect preparation' for Hoylake Open</span></p></span></a>";
        Document document = Jsoup.parse(html);
        Elements links = document.select("a[href].ssrcss-6m4230-PromoLink e1f5wbog1"); 
            for (Element link : links) {
                System.out.println("my beutiful link : " + link.attr("href")); 
            }
    }
}


// OUTPUT:


// EXPECTED OUTPUT:
   my beutiful link : /sport/golf/66227692\

>Solution :

You are missing second . (dot) in the class selector. This should work:

 Elements links = document.select("a[href].ssrcss-6m4230-PromoLink.e1f5wbog1")

which means: Select a having href attribute and class ssrcss-6m4230-PromoLink and class e1f5wbog1

For what you have did a[href].ssrcss-6m4230-PromoLink e1f5wbog1 means: select a having href attribute and class ssrcss-6m4230-PromoLink. Then select all of its children at any depth having tag e1f5wbog1

Makes a difference, no ?? 😉

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading