-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
108 lines (98 loc) · 4.27 KB
/
Copy pathExample.java
File metadata and controls
108 lines (98 loc) · 4.27 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Google Scraper — Scrapeless Scraping API (Java example)
//
// Docs: https://apidocs.scrapeless.com/doc-800321
// https://apidocs.scrapeless.com/doc-1275927
// Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
//
// The Google actor (scraper.google.search) selects a search vertical via the
// tbm input field:
// web (default) | images (isch) | local (lcl) | video (vid) | shopping (shop) | news (nws)
//
// Run (Java 11+, uses the built-in HttpClient):
// export SCRAPELESS_API_TOKEN="your_api_token"
// java Example.java # defaults to the "web" vertical
// java Example.java images # or: web | images | local | video | shopping
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;
public class Example {
private static final String API_URL = "https://api.scrapeless.com/api/v1/scraper/request";
// Ready-to-use request bodies (actor + input) for each search vertical, as raw JSON.
private static final Map<String, String> SAMPLE_BODIES = Map.of(
"web", """
{ "actor": "scraper.google.search", "input": {
"q": "coffee",
"hl": "en",
"gl": "us",
"google_domain": "google.com"
}}""",
"images", """
{ "actor": "scraper.google.search", "input": {
"q": "Apple Iphone16",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"tbm": "isch"
}}""",
"local", """
{ "actor": "scraper.google.search", "input": {
"q": "Coffee",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"tbm": "lcl"
}}""",
"video", """
{ "actor": "scraper.google.search", "input": {
"q": "Coffee",
"google_domain": "google.com",
"start": 0,
"num": 10,
"tbm": "vid"
}}""",
"shopping", """
{ "actor": "scraper.google.search", "input": {
"q": "Coffee",
"google_domain": "google.com",
"start": 0,
"num": 10,
"tbm": "shop"
}}"""
);
public static void main(String[] args) throws Exception {
String vertical = args.length > 0 ? args[0] : "web";
String body = SAMPLE_BODIES.get(vertical);
if (body == null) {
System.out.println("Unknown vertical '" + vertical + "'. Choose one of: web, images, local, video, shopping");
System.exit(1);
}
String apiToken = System.getenv().getOrDefault("SCRAPELESS_API_TOKEN", "YOUR_API_TOKEN");
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.timeout(Duration.ofSeconds(180))
.header("Content-Type", "application/json")
.header("x-api-token", apiToken)
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// The Scraping API distinguishes scenarios by HTTP status code.
switch (response.statusCode()) {
case 200 -> // Synchronous success: the body is the scraped SERP data.
System.out.println("[200] Success — '" + vertical + "' results received.\n" + response.body());
case 201 -> // Task accepted but still running; retrieve later by task id (see docs).
System.out.println("[201] Task in progress (async). Body:\n" + response.body());
case 400 -> // Scraping failed — inspect code/message in the body.
System.out.println("[400] Bad request (scraping failed). Body:\n" + response.body());
default -> {
System.out.println("[" + response.statusCode() + "] Unexpected response:\n" + response.body());
System.exit(1);
}
}
}
}