-
-
Save AnthoniG/41a9c12c18467ed514a3e974a0265d86 to your computer and use it in GitHub Desktop.
A simple web scraping with Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Crawler.kt | |
import kotlinx.coroutines.* | |
import org.json.JSONObject | |
import org.jsoup.Jsoup | |
import org.jsoup.nodes.Document | |
import org.jsoup.nodes.Element | |
import org.jsoup.select.Elements | |
class Crawler { | |
suspend fun pipeline(urls: List<String>, selector: String): List<Elements> = coroutineScope { | |
retrieveAllHtmlDocumentAsync(urls).awaitAll().map { parse(it, selector) } | |
} | |
private fun parse(document: Document, selector: String): Elements { | |
return document.select(selector) | |
} | |
private suspend fun retrieveAllHtmlDocumentAsync(urls: List<String>): List<Deferred<Document>> = coroutineScope { | |
urls.map { url -> | |
async { | |
Jsoup.parse(khttp.get(url).text) | |
} | |
} | |
} | |
private fun toJson(element: Element): JSONObject { | |
return JSONObject(element) | |
} | |
} | |
// Skrotlin.kt - main | |
import kotlin.system.measureTimeMillis | |
suspend fun main(args: Array<String>) { | |
val vanDelay = "http://slowwly.robertomurray.co.uk/delay/3000/url" | |
val totalTime = measureTimeMillis { | |
val crawler = Crawler() | |
val result = crawler.pipeline(listOf("$vanDelay/https://http.cat", | |
"$vanDelay/https://httpstatusdogs.com/", | |
"$vanDelay/https://gympass.com", | |
"$vanDelay/https://google.com", | |
"$vanDelay/https://stackoverflow.com"), "div") | |
println(result) | |
println(result.size) | |
} | |
println("total time: $totalTime") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment