-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
143 lines (129 loc) · 3.59 KB
/
Copy pathexample.go
File metadata and controls
143 lines (129 loc) · 3.59 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Google Scraper — Scrapeless Scraping API (Go 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:
//
// export SCRAPELESS_API_TOKEN="your_api_token"
// go run example.go # defaults to the "web" vertical
// go run example.go images # or: web | images | local | video | shopping
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)
const apiURL = "https://api.scrapeless.com/api/v1/scraper/request"
// sampleInputs holds ready-to-use input payloads for each search vertical.
var sampleInputs = map[string]map[string]any{
"web": {
"q": "coffee",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
},
"images": {
"q": "Apple Iphone16",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"tbm": "isch",
},
"local": {
"q": "Coffee",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"tbm": "lcl",
},
"video": {
"q": "Coffee",
"google_domain": "google.com",
"start": 0,
"num": 10,
"tbm": "vid",
},
"shopping": {
"q": "Coffee",
"google_domain": "google.com",
"start": 0,
"num": 10,
"tbm": "shop",
},
}
func main() {
vertical := "web"
if len(os.Args) > 1 {
vertical = os.Args[1]
}
input, ok := sampleInputs[vertical]
if !ok {
fmt.Printf("Unknown vertical %q. Choose one of: web, images, local, video, shopping\n", vertical)
os.Exit(1)
}
apiToken := os.Getenv("SCRAPELESS_API_TOKEN")
if apiToken == "" {
apiToken = "YOUR_API_TOKEN"
}
body, err := json.Marshal(map[string]any{"actor": "scraper.google.search", "input": input})
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-token", apiToken)
client := &http.Client{Timeout: 180 * time.Second}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// The Scraping API distinguishes scenarios by HTTP status code.
switch resp.StatusCode {
case http.StatusOK: // 200 — synchronous success, body is the scraped SERP data.
fmt.Printf("[200] Success — '%s' results received.\n", vertical)
preview := raw
if len(preview) > 1500 {
preview = preview[:1500]
}
fmt.Printf("\n Raw response (truncated to 1500 chars):\n %s\n", preview)
case http.StatusCreated: // 201 — task accepted, still running.
var task struct {
Message string `json:"message"`
TaskID string `json:"taskId"`
}
_ = json.Unmarshal(raw, &task)
fmt.Printf("[201] Task in progress — message: %s, taskId: %s\n", task.Message, task.TaskID)
fmt.Println(" Fetch the result later using the task id (see docs).")
case http.StatusBadRequest: // 400 — scraping failed.
var e struct {
Code int `json:"code"`
Message string `json:"message"`
}
_ = json.Unmarshal(raw, &e)
fmt.Printf("[400] Bad request — code: %d, message: %s\n", e.Code, e.Message)
default:
fmt.Printf("[%d] Unexpected response:\n%s\n", resp.StatusCode, raw)
os.Exit(1)
}
}