Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use Java Jsoup Tutorial | 100% perfect for beginners

December 16, 2021
in Java, Downloads
Reading Time: 5 mins read
2
Java Jsoup Tutorial
1.2k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • Java Jsoup Tutorial
  • Jsoup Tutorial Java
    • Jsoup Ant
    • Jsoup Maven
    • Gradle
  • Jsoup Example
    • Example: Load a Document from a URL
      • Output
    • Example: Data extraction from a document
      • Output
  • YouTube Video

Java Jsoup Tutorial

Java Jsoup tutorial is a powerful Java library for extracting and manipulating data from websites using HTML5 DOM methods and CSS selectors. This library is designed to scrape and parse HTML from a URL, but it can also find and extract data through DOM traversal or CSS selectors. It is also capable of manipulating HTML elements, attributes, and text. To avoid XSS attacks, it can also clean the content submitted by user.

Jsoup Example

You may use this Java Library to develop your own Java application to scrape data from a URL, and if you want to learn more Java programming tutorials, you can go here to check out other programming tutorials. In this Jsoup tutorial, I will demonstrate how to use this Library or API (Application Programming Interface).

Jsoup Tutorial Java

In this Jsoup Tutorial Java, You will learn how to download and integrate the Java library into your Java program. If you are using Java Ant, Maven, or Java Gradle, please continue reading to learn how to download the library. Let’s start with Java Ant and then go on to Maven and Gradle.

Jsoup Ant

If you are using Jsoup Ant, you will need to download the jar file (core library). After you have downloaded the jar file, you must place it in your project library or the global library. To include the jar file into your project, go to the Libraries section and add the jar file. There is another method to add the jar file; try it if you don’t want to locate the jar file every time you use it. Follow the steps below if you are using NetBeans.

  1. Go to Tools
  2. Click on Libraries
  3. Click on New Library and enter the Library name
  4. Add the Jar at the Classpath tab

That’s how you add the jar file to your NetBeans IDE’s global Libraries.

Jsoup Maven

If you want to use Jsoup Maven, you do not need to download the jar file. Simply insert the dependencies into your Java project, insert the following code into your POM’s <dependencies> section. Simply go to jsoup.org if you don’t know the latest version of the library.

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

Gradle

Add the following code, if you are using Java Gradle.

implementation 'org.jsoup:jsoup:1.14.3'

We can use this code below to see if the code is successfully linked to the specified website. The code example below will tell you if we successfully loaded the website.

Document doc = Jsoup.connect("https://kensoftph.com").get();
System.out.println(doc.title());

The output will be the Title of the specified website.

Jsoup Tutorial

Jsoup Example

In this Jsoup example, I will show a few examples of how to use this Java Library. I know you want to learn more about this library, so go to jsoup.org and navigate to Cookbook. The coverage in the following example will be loading the specified website and extracting some of the specified website’s HTML elements. It is commonly known as web scraping program using Java. Continue reading to learn more.

Example: Load a Document from a URL

Loading a document from a URL is quite simple and straightforward, as seen above. If you haven’t read the entire tutorial, please proceed below to learn how to load a document from a URL.

Document doc = Jsoup.connect("https://kensoftph.com").get();
System.out.println(doc.title());

Output

Kensoft PH

Example: Data extraction from a document

This example demonstrates how to extract data from a document. Extracting data from a document is a little hard, but if you follow the example, it will be much easy. To learn more, I recommend visiting jsoup.org’s Cookbook page.

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * FXML Controller class
 *
 * @author KENSOFT
 */
public class FX_ScraperController implements Initializable {

    @FXML
    private Label lblFrom;
    @FXML
    private ListView<String> listView;
    @FXML
    private Label lblSelectedItem;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            // loading the URL
            Document doc = Jsoup.connect("https://kensoftph.com").get();
            //System.out.println(doc.title());
            lblFrom.setText("Contents from: "+doc.title());
            
            //Extracting the H3 tags and add the items to the ListView
            Elements elements = doc.getElementsByTag("h3");
            for(Element element : elements){
                String items = element.text();
                listView.getItems().add(items);
                listView.getSelectionModel().selectedItemProperty().addListener(listener ->{
                    String item = listView.getSelectionModel().getSelectedItem();
                    lblSelectedItem.setText("Selected item: "+item);
                });
            }
        } catch (IOException ex) {
            Logger.getLogger(FX_ScraperController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Output

This is the output of extracting a data from an HTML document loaded from a URL.

Java Jsoup Tutorial

If you wish to download the JavaFX Project of the Simple Web Scraping application. I’ve provided a download link below so you can quickly get started and tweak it yourself. If you like my tutorials, don’t hesitate to click here or watch my YouTube Video below to learn more via video based in this article.

Download JavaFX Project

YouTube Video

YouTube video

Tags: Java Jsoup TutorialJsoupJsoup Tutorial
Previous Post

Window event in JavaFX | 100% Best for beginners

Next Post

Comments in Java | 100% Perfect for beginner

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
216
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
590
Next Post
Comments in Java

Comments in Java | 100% Perfect for beginner

Read file in Java

How to read file in Java | 100% Perfect for beginners

Layout in JavaFX

Working with layout in JavaFX | 100% Perfect for beginners

Comments 2

  1. Avatar of Lei Lei says:
    3 years ago

    Thanks bro!!!

    Reply
    • Avatar of KENSOFT KENSOFT says:
      3 years ago

      Glad it was helpful

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.