Home > OS >  How to scrape a simple text in kotlin?
How to scrape a simple text in kotlin?

Time:11-05

I know how to scrape by beautifulsoup library in python. but now I need to scrape in kotlin and I don't know how.

I just want to get this simple text, which is a simple echo in PHP.

I tried to use the Jsoup library, but I got Null every time. I don't know why.

it was my Jsoup code in kotlin:

    val doc: Document = Jsoup.connect("https://rezaapp.downloadseriesmovie.ir/maintxt.php").get()
    val text: Elements = doc.select("body")
    println(text)

How can I do it?

CodePudding user response:

If I understand correctly, you're after the pure response text, without any HTML parsing. This makes sense because the response of your URL doesn't actually have any HTML.

JSoup is a library that helps parse HTML pages, so if the response is not HTML, there may be cases where you get null (for instance, there is no <body> element in the response, so I wouldn't be surprised that doc.select("body") returns null).

Since you mention using JSoup, I'm assuming you're running Kotlin on the JVM.

In this case, if you're only interested in pure response text, you can make use of Java's URL class, and use Kotlin's readText helper:

import java.net.URL

fun main() {
    val input = URL("https://rezaapp.downloadseriesmovie.ir/maintxt.php").readText()
    println(input) // prints https://rezaapp.downloadseriesmovie.ir/
}

That being said, if you're really considering reading web pages, I would consider using JSoup or some HTTP client like Ktor a parsing library for HTML.

  • Related