-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
94 lines (84 loc) · 3.85 KB
/
Copy pathExample.java
File metadata and controls
94 lines (84 loc) · 3.85 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
// Amazon Scraper — Scrapeless Scraping API (Java example)
//
// Docs: https://apidocs.scrapeless.com/doc-857373
// Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
//
// The Amazon actor supports four scrape types selected via input.type:
// product | seller | keywords | rufus
//
// Run (Java 11+, uses the built-in HttpClient):
// export SCRAPELESS_API_TOKEN="your_api_token"
// java Example.java # defaults to the "product" type
// java Example.java keywords # or pass a type: product | seller | keywords | rufus
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 scrape type, as raw JSON.
private static final Map<String, String> SAMPLE_BODIES = Map.of(
"product", """
{ "actor": "scraper.amazon", "input": {
"type": "product",
"url": "https://www.amazon.com/dp/B0BQXHK363",
"zip_code": ""
}}""",
"seller", """
{ "actor": "scraper.amazon", "input": {
"type": "seller",
"url": "https://www.amazon.com/sp?seller=A2XZ7JICGUQ1CX",
"zip_code": ""
}}""",
"keywords", """
{ "actor": "scraper.amazon", "input": {
"type": "keywords",
"keywords": "Iphone+14+Pro+512GB",
"page": "1",
"domain": "com",
"zip_code": ""
}}""",
"rufus", """
{ "actor": "scraper.amazon", "input": {
"type": "rufus",
"keywords": "macbook",
"domain": "www.amazon.es",
"page": "1"
}}"""
);
public static void main(String[] args) throws Exception {
String scrapeType = args.length > 0 ? args[0] : "product";
String body = SAMPLE_BODIES.get(scrapeType);
if (body == null) {
System.out.println("Unknown type '" + scrapeType + "'. Choose one of: product, seller, keywords, rufus");
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 Amazon actor distinguishes scenarios by HTTP status code.
switch (response.statusCode()) {
case 200 -> // Synchronous success: the body is the scraped data (shape depends on type).
System.out.println("[200] Success — '" + scrapeType + "' data 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);
}
}
}
}