jsoup extract Website Title in Java
Tags: jsoup HTML Parser
In this post, we show you how to use the jsoup library in a Java application to extract the title of a website.
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
How to use jsoup to extract title of website
The jsoup library provides Document.title() method to get the title of a HTML document. The following Java code example to show you how to get and parse the website content into a Document object. And using the title() method to get the title of a website.
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class GetWebsiteTitle {
public static void main(String... args) {
try {
String websiteUrl = "https://simplesolution.dev";
Document document = Jsoup.connect(websiteUrl).get();
String title = document.title();
System.out.println(title);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Simple Solution
Happy Coding 😊
Related Articles
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 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 CSS class name in Java
Clean HTML String to get Safe HTML from Untrusted HTML in Java using jsoup