123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

How To Build A Web Scraping Api Using Java, Spring Boot, And Jsoup?

Profile Picture
By Author: 3i Data Scraping
Total Articles: 34
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

At 3i Data Scraping, we will create an API for scraping data from a couple of vehicle selling sites as well as extract the ads depending on vehicle models that we pass for an API. This type of API could be used from the UI as well as show different ads from various websites in one place.

Web Scraping
IntelliJ as IDE of option
Maven 3.0+ as a building tool
JDK 1.8+
Getting Started
Initially, we require to initialize the project using a spring initializer

It can be done by visiting http://start.spring.io/

Ensure to choose the given dependencies also:

Lombok: Java library, which makes a code cleaner as well as discards boilerplate codes.
Spring WEB: It is a product of the Spring community, with a focus on making document-driven web services.
After starting the project, we would be utilizing two-third party libraries JSOUP as well as Apache commons. The dependencies could be added in the pom.xml file.






Analyze HTML to Extract Data
Before starting the implementation of API, we need to visit https://riyasewana.com/ and https://ikman.lk/ to locate data, which we need to extract from these sites.

We can perform that by launching the given sites on the browser as well as inspecting HTML code with Dev tools.

If you are using Chrome, just right-click on the page as well as choose inspect.

Its result will look like this:

After opening different websites we need to navigate through HTML for identifying a DOM where the ad list is positioned. These identified elements would be utilized in the spring boot project for getting relevant data.

From navigating through ikman.lk HTML, it’s easy to see a list of ads are positioned under a class name’s list — 3NxGO.

After that, we need to perform the same with Riyasewana.com where ad data is positioned under a div with id content.

After recognizing all the data, let’s create our API for scraping the data!!!.

Initially, we need to define website URLs in the file called application.yml/application.properties

urls: https://ikman.lk/en/ads/sri-lanka/vehicles?sort=relevance&buy_now=0&urgent=0&query=,https://riyasewana.com/search/
After that, create an easy model class for mapping data using HTML.

package com.scraper.api.model;

import lombok.Data;

public class ResponseDTO {
String title;
String url;
In the given code, we utilize Data annotation generation setters and getters for attributes.

After that, it’s time to create a service layer as well as scrape data from these websites.

package com.scraper.api.service;

import com.scraper.api.model.ResponseDTO;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ScraperServiceImpl implements ScraperService {
//Reading data from property file to a list
List urls;

public Set getVehicleByModel(String vehicleModel) {
//Using a set here to only store unique elements
Set responseDTOS = new HashSet();
//Traversing through the urls
for (String url: urls) {

if (url.contains("ikman")) {
//method to extract data from Ikman.lk
extractDataFromIkman(responseDTOS, url + vehicleModel);
} else if (url.contains("riyasewana")) {
//method to extract Data from riyasewana.com
extractDataFromRiyasewana(responseDTOS, url + vehicleModel);


return responseDTOS;

private void extractDataFromRiyasewana(Set responseDTOS, String url) {

try {
//loading the HTML to a Document Object
Document document = Jsoup.connect(url).get();
//Selecting the element which contains the ad list
Element element = document.getElementById("content");
//getting all the tag elements inside the content div tag
Elements elements = element.getElementsByTag("a");
//traversing through the elements
for (Element ads: elements) {
ResponseDTO responseDTO = new ResponseDTO();

if (!StringUtils.isEmpty(ads.attr("title")) ) {
//mapping data to the model class
if (responseDTO.getUrl() != null) responseDTOS.add(responseDTO);
} catch (IOException ex) {

private void extractDataFromIkman(Set responseDTOS, String url) {
try {
//loading the HTML to a Document Object
Document document = Jsoup.connect(url).get();
//Selecting the element which contains the ad list
Element element = document.getElementsByClass("list--3NxGO").first();
//getting all the
tag elements inside the list- -3NxGO class
Elements elements = element.getElementsByTag("a");

for (Element ads: elements) {

ResponseDTO responseDTO = new ResponseDTO();

if (StringUtils.isNotEmpty(ads.attr("href"))) {
//mapping data to our model class
responseDTO.setUrl("https://ikman.lk"+ ads.attr("href"));
if (responseDTO.getUrl() != null) responseDTOS.add(responseDTO);

} catch (IOException ex) {

After writing the scraping logic for a service layer, we can now implement the RestController for fetching data from these websites.

package com.scraper.api.controller;

import com.scraper.api.model.ResponseDTO;
import com.scraper.api.service.ScraperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;

@RequestMapping(path = "/")
public class ScraperController {

ScraperService scraperService;

@GetMapping(path = "/{vehicleModel}")
public Set getVehicleByModel(@PathVariable String vehicleModel) {
return scraperService.getVehicleByModel(vehicleModel);
When everything is completed. We need to Run this Project as well as a test this API!

Go to the RestClient as well as call API through offering a vehicle model.

For example http://localhost:8080/axio

Here, you can observe that you have all the ad URLs as well as titles associated to given vehicle models from both these websites.

In this blog, you have learned about how to manipulate the HTML document using jsoup as well as spring boot to extract data from these two websites. The next step will be:

Improving this API to help pagination in these websites.

Implementing the UI for consuming the API

For more information on building web scraping API with Java, Spring Boot, or Jsoup, you can contact 3i Data Scraping or ask for a free quote!

More About the Author

3i Data Scraping is an Experienced Web Scraping Services Company in the USA. We are Providing a Complete Range of Web Scraping, Mobile App Scraping, Data Extraction, Data Mining, and Real-Time Data Scraping (API) Services. We have 11+ Years of Experience in Providing Website Data Scraping Solutions to Hundreds of Customers Worldwide.

Add Comment

Computer Programming Articles

1. A Guide To Know All About Full Stack Developers
Author: shelly

2. How To Scrape Rentals Websites Using Beautifulsoup And Python?
Author: 3i Data Scraping

3. Ip Blocking: Quick Guide To Protect Your Network Or Website
Author: Camimi Morales

4. Xamarin Vs Flutter – Which Cross-platform Framework Is Ideal For You?
Author: Cross-platform framework

5. How To Do Web Scraping Of Food Delivery Data In A Seamless Manner?
Author: 3i Data Scraping

6. How Does Blockchain Help With Digital Identity?
Author: Sataware Technologies

7. Significant Telemedicine App Monetizing Strategies You Need To Consider In 2022!
Author: Shelly

8. Why Mobile App Development Is Important For Other Businesses
Author: Saran

9. Great Things About Being A Full Stack Programmer As Part Of Your It Profession
Author: Daniel Wallis

10. How Important The Big Data Is For The Tourism Industry?
Author: 3i Data Scraping

11. Guidance On Nft Creation And Monetization Strategies!
Author: Shelly

12. Infographic: Why Users Uninstall Your Mobile App
Author: sataware

13. Sustainability Services — Livsite
Author: Livsite

14. How To Create A Shopify App Using Php?
Author: Sataware Technologies

15. Product Development Process – Overview And Its Importance
Author: Satya K Vivek

Login To Account
Login Email:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: