jsoup Get HTML elements by CSS class name in Java

Tags: jsoup HTML Parser

In this post, we are going to learn how to use jsoup Java library to get all HTML elements by CSS class name.

Add jsoup library to your Java project

To use jsoup Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile 'org.jsoup:jsoup:1.13.1'

To use jsoup Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

To download the jsoup-1.13.1.jar file you can visit jsoup download page at jsoup.org/download

Sample HTML File

For example, we have the sample.html file as below.

<!DOCTYPE html>
<html>
<body>
    <p class="article">Java Tutorials</p>
    <p class="article">Spring Boot Tutorials</p>
    <p class="article">jsoup Tutorials</p>
</body>
</html>

Using Document.getElementsByClass() method

In the following Java code example we use jsoup’s Document.getElementsByClass() method to get all elements by given class name.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

public class GetElementByCssClassExample1 {
    public static void main(String... args) {
        try {
            File file = new File("sample.html");
            Document document = Jsoup.parse(file, "UTF-8");

            Elements elements = document.getElementsByClass("article");

            for(Element element : elements) {
                System.out.println(element.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Java Tutorials
Spring Boot Tutorials
jsoup Tutorials

Using Document.select() method

The jsoup library also provides methods to get elements using CSS selector.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

public class GetElementByCssClassExample2 {
    public static void main(String... args) {
        try {
            File file = new File("sample.html");
            Document document = Jsoup.parse(file, "UTF-8");

            Elements elements = document.select(".article");

            for(Element element : elements) {
                System.out.println(element.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Java Tutorials
Spring Boot Tutorials
jsoup Tutorials

Happy Coding 😊

jsoup Get HTML Element by ID in Java

jsoup Get HTML Elements by Tag Name in Java

jsoup extract CSS class name of HTML element in Java

jsoup extract ID and name of HTML element in Java

jsoup extract text and attributes of HTML element in Java

jsoup extract Inner and Outer HTML of HTML Element in Java

jsoup extract JavaScript from HTML script element in Java

jsoup extract custom data attributes of HTML5 Element in Java

jsoup extract Website Title in Java

jsoup parse HTML Document from a Java String

jsoup parse HTML Document from an URL in Java

jsoup parse HTML Document from a File and InputStream in Java

Pretty Printing HTML String in Java using jsoup

Extract All Links of a web page in Java using jsoup

jsoup Get HTML Elements by Attribute Value in Java

Clean HTML String to get Safe HTML from Untrusted HTML in Java using jsoup

jsoup Get All HTML Elements in Java