skrape{it}
Search…
Creating a RESTful API (Spring-Boot)
This example assumes a working Spring-Boot setup.
This is a basic example on how you could build a REST API directly return data from a scraped website as JSON:
1
import it.skrape.extract
2
import it.skrape.selects.elements
3
import it.skrape.selects.element
4
import it.skrape.skrape
5
import org.springframework.web.bind.annotation.GetMapping
6
import org.springframework.web.bind.annotation.RestController
7
8
@RestController
9
class MyController {
10
11
@GetMapping("/user-data")
12
fun extractGithubUserData() =
13
skrape {
14
url = "https://github.com/skrapeit"
15
extract {
16
MyScrapedData(
17
userName = element(".h-card .p-nickname").text(),
18
repositoryNames = elements("span.repo").map { it.text() }
19
)
20
}
21
}
22
}
23
24
data class MyScrapedData(val userName: String, val repositoryNames: List<String>)
Copied!
This will return the following JSON as response body when /user-data is getting called:
1
{
2
"userName": "skrapeit",
3
"repositoryNames": [
4
"skrape.it",
5
"skrapeit-docs",
6
"skrapeit-ktor-extension",
7
"skrapeit-mockmvc-extension",
8
"skrapeit-parent"
9
]
10
}
Copied!
Last modified 2yr ago
Copy link