Jsoup for Java: The Complete Guide to HTML Parsing
1. What is Jsoup and Why Use It?
Jsoup is a powerful, open-source Java library designed for parsing, extracting data from, and manipulating HTML documents. It provides developers with a clean, intuitive API that lets you work with HTML as easily as you would with the DOM tree in a browser. If you've ever had to write regular expressions to extract data from web pages, you know how fragile and hard to maintain that approach can be. Jsoup solves this by offering a robust and flexible tool.
Jsoup's core function is HTML parsing. It can take a string, a URL, or a file as input and convert it into a Document Object Model (DOM). Once parsed, you can use CSS selectors (similar to jQuery) or DOM traversal to find and extract specific elements, their attributes, and text content. Jsoup can also sanitize HTML to remove potentially dangerous elements (like XSS attacks) and modify the document by adding, removing, or changing tags and their content.
Why is this useful? Jsoup has a wide range of applications: from building simple web scrapers for data collection and price monitoring to creating complex content analysis systems, testing web applications, and integrating with CMS platforms. Jsoup is the de facto standard for working with HTML in the Java world, and knowing it is essential for any Java developer working with web technologies.
2. Installing Jsoup
Jsoup is distributed as a JAR file, but the easiest way to add it is through a build system. Below are the commands for Maven and Gradle.
Maven (pom.xml)
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
Gradle (build.gradle)
implementation 'org.jsoup:jsoup:1.17.2'
If you're not using a build system, download the JAR file from the official website and add it to your project's classpath.
3. Quick Start: A Minimal Working Example
Let's write a simple program that loads Google's homepage and prints its title.
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class QuickStart {
public static void main(String[] args) throws Exception {
// 1. Load the HTML document from a URL
Document doc = Jsoup.connect("https://www.google.com").get();
// 2. Extract the page title (content of the <title> tag)
String title = doc.title();
// 3. Print the result
System.out.println("Page title: " + title);
}
}
This code makes an HTTP GET request to the specified URL, parses the returned HTML, and returns a Document object. The title() method is a convenient way to get the content of the <title> tag. The output will be something like: Page title: Google.
4. Core Jsoup Methods and Classes
Let's explore the key classes and methods of the library. All examples assume you have already imported org.jsoup.Jsoup and org.jsoup.nodes.*.
4.1. Jsoup.connect(String url)
Signature: public static Connection connect(String url)
What it does: Creates a new HTTP connection to the specified URL. Returns a Connection object, which you can use to configure the request (e.g., set headers, timeouts) and then execute it.
Also in library
Compression for JavaScript (Node.js): Complete Guide with Examples
Swinject for Swift: Complete Guide with Examples