Use Jsoup to select an HTML element without a class

Consider an html document like this

<div>
    <p>...</p>
    <p>...</p>
    ...
    <p class="random_class_name">...</p>
    ...
</div>
Run codeHide result

How can we select all elements p, but excluding an element pwith a random_class_nameclass?

+4
source share
2 answers
Elements ps = body.select("p:not(.random_class_name)");

You can use pseudo selector :not

If the class name is unknown, you can still use a similar expression:

Elements ps = body.select("p:not([class])");

In the second example, I use the attribute selector [], in the first - the usual syntax for classes.

See Jsoup doc on css selectors

+8
source
Document doc = Jsoup.parse(htmlValue);
    Elements pElements = doc.select("p");         
    for (Element element : pElements) {
        String class = element.attr("class");
        if(class == null){
            //.....
        }else{
             //.....
        }
    }
+1
source

Source: https://habr.com/ru/post/1598124/


All Articles