jsoup parse HTML Document from an URL in Java
Tags: Java jsoup HTML Parser
Introduction
In this tutorial we will explore how to use the jsoup library in Java program to parse HTML from a given URL into a jsoup Document object.
What is jsoup?
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors.
For more information about the library you can visit jsoup homepage at jsoup.org
Add jsoup library to your 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
Parse HTML from an URL into jsoup Document
Jsoup provides Jsoup.connect() static method to create a Connection to an URL, from the return Connection we can send different HTTP requests to fetch HTML from remote server such as get(), post().
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class JsoupConnectExample {
public static void main(String... args) {
try {
String url = "https://simplesolution.dev";
Connection connection = Jsoup.connect(url);
Document document = connection.get();
Elements linkElements = document.getElementsByTag("a");
for(Element element : linkElements) {
System.out.println(element.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Simple Solution
Java
Spring Boot
Java Code Examples
...
Happy Coding 😊
Related Articles
jsoup parse HTML Document from a Java String
jsoup parse HTML Document from a File and InputStream 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
Pretty Printing HTML String in Java using jsoup