In this article, we will learn how to use Kotlin and the Jsoup library to download all the images from a Wikipedia page.
—-
Overview
The goal is to extract the names, breed groups, local names, and image URLs for all dog breeds listed on this Wikipedia page. We will store the image URLs, download the images and save them to a local folder.
Here are the key steps we will cover:
- Import required packages
- Send HTTP request to fetch the Wikipedia page
- Parse the page HTML using Jsoup
- Find the table with dog breed data using a CSS selector
- Iterate through the table rows
- Extract data from each column
- Download images and save locally
- Print/process extracted data
Let's go through each of these steps in detail.
Imports
We need these packages:
import java.net.HttpURLConnection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
Send HTTP Request
To download the web page:
val url = "<https://commons.wikimedia.org/wiki/List_of_dog_breeds>"
val connection = URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty("User-Agent", "KotlinScraper")
val document = connection.inputStream.bufferedReader().use{ it.readText() }
We open a connection and provide a custom user-agent header.
Parse HTML
To parse the HTML:
val html: Document = Jsoup.parse(document)
The Jsoup
Find Breed Table
We use a CSS selector to find the table element:
val table = html.select("table.wikitable.sortable")
This selects the We can iterate through the rows like this: We loop through each Inside the loop, we extract the column data: We use To download and save images: We open the image stream and save it to a file. We can store the extracted data in lists: The lists can then be processed as needed. And that's it! Here is the full code: This provides a complete Kotlin solution using Jsoup to scrape data and images from HTML tables. The same approach can apply to many websites. While these examples are great for learning, scraping production-level sites can pose challenges like CAPTCHAs, IP blocks, and bot detection. Rotating proxies and automated CAPTCHA solving can help. Proxies API offers a simple API for rendering pages with built-in proxy rotation, CAPTCHA solving, and evasion of IP blocks. You can fetch rendered pages in any language without configuring browsers or proxies yourself. This allows scraping at scale without headaches of IP blocks. Proxies API has a free tier to get started. Check out the API and sign up for an API key to supercharge your web scraping. With the power of Proxies API combined with Python libraries like Beautiful Soup, you can scrape data at scale without getting blocked.
Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you
curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com" <!doctype html> Enter your email below to claim your free API key: tag with the required CSS classes.
Iterate Through Rows
for (row in table.select("tr")) {
// Extract data
}
element within the table. Extract Column Data
val cells = row.select("td, th")
val name = cells[0].select("a").text()
val group = cells[1].text()
val localName = cells[2].select("span").text() ?: ""
val img = cells[3].select("img").attr("src")
Download Images
if (img != "") {
val imageStream = URL(img).openStream()
Files.copy(imageStream, Paths.get("dog_images/$name.jpg"))
}
Store Extracted Data
names.add(name)
groups.add(group)
localNames.add(localName)
images.add(img)
// Imports
import java.net.HttpURLConnection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
// Lists to store data
val names = mutableListOf<String>()
val groups = mutableListOf<String>()
val localNames = mutableListOf<String>()
val images = mutableListOf<String>()
// Fetch HTML
val url = "<https://commons.wikimedia.org/wiki/List_of_dog_breeds>"
val connection = URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty("User-Agent", "KotlinScraper")
val document = connection.inputStream.bufferedReader().use{ it.readText() }
// Parse HTML
val html: Document = Jsoup.parse(document)
// Find table
val table = html.select("table.wikitable.sortable")
// Iterate rows
for (row in table.select("tr")) {
// Get cells
val cells = row.select("td, th")
// Extract data
val name = cells[0].select("a").text()
val group = cells[1].text()
val localName = cells[2].select("span").text() ?: ""
val img = cells[3].select("img").attr("src")
// Download image
if (img != "") {
val imageStream = URL(img).openStream()
Files.copy(imageStream, Paths.get("dog_images/$name.jpg"))
}
// Store data
names.add(name)
groups.add(group)
localNames.add(localName)
images.add(img)
}
Browse by tags:
Browse by language:
The easiest way to do Web Scraping
Try ProxiesAPI for free
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
...Don't leave just yet!