HTML Parsing in Kotlin: A Complete Overview of Tools and Libraries
Kotlin is a modern programming language that runs on JVM, Android, and in the browser. Thanks to Java compatibility and a powerful standard library, Kotlin offers several efficient ways to parse HTML. In this article, we will review the main libraries for HTML parsing in Kotlin, their features, and provide practical code examples.
1. Jsoup — Universal HTML Parser for Kotlin
Jsoup is the most popular library for HTML parsing on JVM. It works great with Kotlin thanks to full Java compatibility. Jsoup allows you to load HTML from a URL, file, or string, and then extract data using CSS selectors and DOM navigation.
Installing Jsoup in a Kotlin Project
Add the dependency to build.gradle.kts:
dependencies { implementation("org.jsoup:jsoup:1.17.2")}Example of HTML Parsing with Jsoup
import org.jsoup.Jsoupimport org.jsoup.nodes.Document
fun main() { val url = "https://example.com" val doc: Document = Jsoup.connect(url).get() val title = doc.title() println("Page title: $title") // Extract all links val links = doc.select("a[href]") links.forEach { link -> println("Link: ${link.attr("abs:href")} — ${link.text()}") } // Parsing from a string val html = "<div class='content'>Hello, Kotlin!</div>" val parsed = Jsoup.parse(html) val text = parsed.select(".content").text() println("Text from HTML: $text")}2. Ktor Client + kotlinx.html — Native Kotlin Approach
For projects using Ktor as an HTTP client, an excellent choice is the combination of ktor-client and kotlinx.html. The former handles page loading, the latter parses HTML into a typed DOM structure.
Dependencies for Ktor and kotlinx.html
dependencies { implementation("io.ktor:ktor-client-core:2.3.7") implementation("io.ktor:ktor-client-cio:2.3.7") implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.11.0")}Example of Parsing with Ktor and kotlinx.html
import io.ktor.client.*import io.ktor.client.request.*import io.ktor.client.statement.*import kotlinx.html.*import kotlinx.html.parser.*
suspend fun parseHtmlWithKtor() { val client = HttpClient() val response: HttpResponse = client.get("https://example.com") val htmlContent = response.bodyAsText() val document = parseHtml(htmlContent) val title = document.head?.title ?: "No title" println("Title: $title") // Extract all paragraphs document.body?.allElements?.filterIsInstance<P>()?.forEach { p -> println("Paragraph: ${p.text}") } client.close()}3. Comparison of Libraries for HTML Parsing in Kotlin
The choice of tool depends on the specific task. Here are the main criteria:
- Jsoup — the best choice for complex parsing with CSS selectors. Suitable for data scraping, extracting tables, forms, and media elements.
- Ktor + kotlinx.html — ideal for Ktor projects requiring lightweight parsing without extra dependencies. Works asynchronously.
- HtmlCleaner — another Java library well compatible with Kotlin. Useful for cleaning "dirty" HTML.
- OkHttp + Jsoup — a combination for fine-tuning HTTP requests with powerful parsing.
4. Advanced Parsing Techniques in Kotlin
Extracting Data Using CSS Selectors (Jsoup)
val doc = Jsoup.connect("https://news.ycombinator.com").get()val stories = doc.select("tr.athing")stories.forEach { story ->