Parsing HTML in Java: Library Overview and Practical Examples
Parsing HTML (extracting data from web pages) is one of the key tasks in modern development. Java provides powerful tools for this purpose. In this article, we will look at three popular libraries: Jsoup, HtmlUnit, and Apache Tika. You will learn how to install them, what tasks they solve, and see working code examples.
Why is HTML parsing needed?
HTML parsing is used for:
- Collecting data from websites (prices, news, contacts).
- Testing web applications (extracting interface elements).
- Automating routine operations (filling forms, checking links).
- Analyzing the SEO structure of websites.
1. Jsoup — the most popular HTML parser for Java
Jsoup is a lightweight, intuitive library for working with HTML. It can load pages, parse them, and extract data using CSS selectors and jQuery-like methods. Jsoup automatically fixes incorrect markup, making it ideal for parsing real websites.
Installation (Maven)
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.17.2</version></dependency>Example: extracting all news headlines
Suppose we need to get all article headlines from a news portal. We use the CSS selector h2.news-title.
import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.select.Elements;
public class JsoupExample { public static void main(String[] args) throws Exception { // Load the HTML page Document doc = Jsoup.connect("https://example.com/news") .userAgent("Mozilla/5.0") .timeout(5000) .get();
// Extract all h2 elements with class "news-title" Elements titles = doc.select("h2.news-title");
// Print the text of each headline for (var title : titles) { System.out.println(title.text()); } }}Features of Jsoup:
- Works without a browser, only HTTP requests.
- Support for CSS selectors (like in jQuery).
- Ability to manipulate the DOM (adding, removing elements).
- Cannot execute JavaScript.
2. HtmlUnit — parsing with JavaScript support
HtmlUnit is a browser emulator in Java. It doesn't just parse HTML, but fully processes the page: executes JavaScript, loads CSS, handles AJAX requests. Ideal for modern SPA websites (React, Angular).
Installation (Maven)
<dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.70.0</version></dependency>Example: getting data after JavaScript loads
Suppose the page has a dynamic block that appears 2 seconds after loading. HtmlUnit will wait for it.
import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.HtmlPage;import com.gargoylesoftware.htmlunit.html.HtmlElement;
public class HtmlUnitExample { public static void main(String[] args) throws Exception { try (WebClient webClient = new WebClient()) { // Disable CSS and logging for speed webClient.getOptions().setCssEnabled(false); webClient.getOptions().setJavaScriptEnabled(true);
// Load the page HtmlPage page = webClient.getPage("https://example.com/dynamic");
Also in library
C# (.NET) Application Configuration: Microsoft.Extensions.Configuration
Machine Learning in Java: Overview of Libraries for Model Development
Machine Learning in Go: Overview of Libraries and Frameworks (Golang)
Working with S3 and Cloud Storage in JavaScript (Node.js) — @aws-sdk/client-s3 Library