-
Notifications
You must be signed in to change notification settings - Fork 119
/
Scraper.kt
32 lines (28 loc) · 1.21 KB
/
Scraper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ai.platon.pulsar.client
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpRequest.BodyPublishers
import java.net.http.HttpResponse.BodyHandlers
import kotlin.system.exitProcess
/**
* A simple scraper which just send an X-SQL request to the pulsar server and get the scrape result
* */
fun main() {
val sql = """
select
dom_first_text(dom, '#productTitle') as `title`,
dom_first_text(dom, '#price tr td:contains(List Price) ~ td') as `listprice`,
dom_first_text(dom, '#price tr td:matches(^Price) ~ td, #price_inside_buybox') as `price`,
array_join_to_string(dom_all_texts(dom, '#wayfinding-breadcrumbs_container ul li a'), '|') as `categories`,
dom_base_uri(dom) as `baseUri`
from
load_and_select('https://www.amazon.com/dp/B0C1H26C46 -i 10s', ':root')
"""
val request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8182/api/x/e"))
.header("Content-Type", "text/plain")
.POST(BodyPublishers.ofString(sql)).build()
val response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()).body()
println(response)
}