Raunak
Raunak

Reputation: 6507

Jsoup not selector not returning result

Trying to use Jsoup selector to select everything in a div with class 'content', but at the same time not select any divs with class social,or media. I know I can do a simple select and loop, but would have expected the :not function to work for my purpose. Perhaps, my selector syntax is wrong.

public static void main(String args[]) throws ParseException {
    String html = "<html>\n" +
            "<body>\n" +
            "<div class=\"content\">\n" +
            "\t<p>some paragraph</p>\n" +
            "\t<div class=\"social media\">\n" +
            "\tfind us on facebook\n" +
            "\t</div\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>";
    Document doc = Jsoup.parse(html);
    Elements elements = doc.select("div.content div:not(.social)");
    System.out.println(elements.text());
}

Expected result: "some paragraph"

Actual result: null

Upvotes: 2

Views: 3639

Answers (1)

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

Your selector as it is, matches divs that do not have class="social" and are childs of div with class="content". To have the expected outcome use this

Elements elements = doc.select("div.content :not(.social)");

Or this

Elements elements = doc.select("div.content").not(".social");

Upvotes: 5

Related Questions