Search code examples
javahtmljsoup

Jsoup element is empty when selecting by class


The html i am parsing is like this:

<article id="1234" class="bg-post ">
....
</article>

When I try parsing it using jsoup I am getting an empty string

Elements e = doc.select("article[class=bg-post ]");

OR using

Elements e = doc.getElementsByClass("bg-post ");

The size of e is 0 and e.toString() is empty.

Does it have something to do with the space at the end of the class name, how do I resolve this issue.

Thank you.

EDIT:

Also tried searching without the space

Elements e = doc.select("article[class=bg-post]");

Still the same problem.


Solution

  • This is a bug affecting JSoup versions before 1.8.2

    You cannot simply select elements by attributes' value if they are not correctly space-normalized.

    If you really have to stick to some old JSoup version, you can workaround it in this less-performant way:

    document.select(".bg-post").select("article")