-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
378 lines (345 loc) · 14.2 KB
/
main.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
// SearchResult holds our scraped data with json annotations.
type SearchResult struct {
SearchRank int `json: rank`
SearchURL string `json: url`
SearchTitle string `json: title`
SearchDesc string `json: desc`
}
// BaseGoogleUrls holds all Google base domains ordered by the country code.
var BaseGoogleUrls = map[string]string{
"com": "https://www.google.com/search?q=",
"ac": "https://www.google.ac/search?q=",
"ad": "https://www.google.ad/search?q=",
"ae": "https://www.google.ae/search?q=",
"af": "https://www.google.com.af/search?q=",
"ag": "https://www.google.com.ag/search?q=",
"ai": "https://www.google.com.ai/search?q=",
"al": "https://www.google.al/search?q=",
"am": "https://www.google.am/search?q=",
"ao": "https://www.google.co.ao/search?q=",
"ar": "https://www.google.com.ar/search?q=",
"as": "https://www.google.as/search?q=",
"at": "https://www.google.at/search?q=",
"au": "https://www.google.com.au/search?q=",
"az": "https://www.google.az/search?q=",
"ba": "https://www.google.ba/search?q=",
"bd": "https://www.google.com.bd/search?q=",
"be": "https://www.google.be/search?q=",
"bf": "https://www.google.bf/search?q=",
"bg": "https://www.google.bg/search?q=",
"bh": "https://www.google.com.bh/search?q=",
"bi": "https://www.google.bi/search?q=",
"bj": "https://www.google.bj/search?q=",
"bn": "https://www.google.com.bn/search?q=",
"bo": "https://www.google.com.bo/search?q=",
"br": "https://www.google.com.br/search?q=",
"bs": "https://www.google.bs/search?q=",
"bt": "https://www.google.bt/search?q=",
"bw": "https://www.google.co.bw/search?q=",
"by": "https://www.google.by/search?q=",
"bz": "https://www.google.com.bz/search?q=",
"ca": "https://www.google.ca/search?q=",
"kh": "https://www.google.com.kh/search?q=",
"cc": "https://www.google.cc/search?q=",
"cd": "https://www.google.cd/search?q=",
"cf": "https://www.google.cf/search?q=",
"cat": "https://www.google.cat/search?q=",
"cg": "https://www.google.cg/search?q=",
"ch": "https://www.google.ch/search?q=",
"ci": "https://www.google.ci/search?q=",
"ck": "https://www.google.co.ck/search?q=",
"cl": "https://www.google.cl/search?q=",
"cm": "https://www.google.cm/search?q=",
"co": "https://www.google.com.co/search?q=",
"cr": "https://www.google.co.cr/search?q=",
"cu": "https://www.google.com.cu/search?q=",
"cv": "https://www.google.cv/search?q=",
"cy": "https://www.google.com.cy/search?q=",
"cz": "https://www.google.cz/search?q=",
"de": "https://www.google.de/search?q=",
"dj": "https://www.google.dj/search?q=",
"dk": "https://www.google.dk/search?q=",
"dm": "https://www.google.dm/search?q=",
"do": "https://www.google.com.do/search?q=",
"dz": "https://www.google.dz/search?q=",
"ec": "https://www.google.com.ec/search?q=",
"ee": "https://www.google.ee/search?q=",
"eg": "https://www.google.com.eg/search?q=",
"es": "https://www.google.es/search?q=",
"et": "https://www.google.com.et/search?q=",
"fi": "https://www.google.fi/search?q=",
"fj": "https://www.google.com.fj/search?q=",
"fm": "https://www.google.fm/search?q=",
"fr": "https://www.google.fr/search?q=",
"ga": "https://www.google.ga/search?q=",
"ge": "https://www.google.ge/search?q=",
"gf": "https://www.google.gf/search?q=",
"gg": "https://www.google.gg/search?q=",
"gh": "https://www.google.com.gh/search?q=",
"gi": "https://www.google.com.gi/search?q=",
"gl": "https://www.google.gl/search?q=",
"gm": "https://www.google.gm/search?q=",
"gp": "https://www.google.gp/search?q=",
"gr": "https://www.google.gr/search?q=",
"gt": "https://www.google.com.gt/search?q=",
"gy": "https://www.google.gy/search?q=",
"hk": "https://www.google.com.hk/search?q=",
"hn": "https://www.google.hn/search?q=",
"hr": "https://www.google.hr/search?q=",
"ht": "https://www.google.ht/search?q=",
"hu": "https://www.google.hu/search?q=",
"id": "https://www.google.co.id/search?q=",
"iq": "https://www.google.iq/search?q=",
"ie": "https://www.google.ie/search?q=",
"il": "https://www.google.co.il/search?q=",
"im": "https://www.google.im/search?q=",
"in": "https://www.google.co.in/search?q=",
"io": "https://www.google.io/search?q=",
"is": "https://www.google.is/search?q=",
"it": "https://www.google.it/search?q=",
"je": "https://www.google.je/search?q=",
"jm": "https://www.google.com.jm/search?q=",
"jo": "https://www.google.jo/search?q=",
"jp": "https://www.google.co.jp/search?q=",
"ke": "https://www.google.co.ke/search?q=",
"ki": "https://www.google.ki/search?q=",
"kg": "https://www.google.kg/search?q=",
"kr": "https://www.google.co.kr/search?q=",
"kw": "https://www.google.com.kw/search?q=",
"kz": "https://www.google.kz/search?q=",
"la": "https://www.google.la/search?q=",
"lb": "https://www.google.com.lb/search?q=",
"lc": "https://www.google.com.lc/search?q=",
"li": "https://www.google.li/search?q=",
"lk": "https://www.google.lk/search?q=",
"ls": "https://www.google.co.ls/search?q=",
"lt": "https://www.google.lt/search?q=",
"lu": "https://www.google.lu/search?q=",
"lv": "https://www.google.lv/search?q=",
"ly": "https://www.google.com.ly/search?q=",
"ma": "https://www.google.co.ma/search?q=",
"md": "https://www.google.md/search?q=",
"me": "https://www.google.me/search?q=",
"mg": "https://www.google.mg/search?q=",
"mk": "https://www.google.mk/search?q=",
"ml": "https://www.google.ml/search?q=",
"mm": "https://www.google.com.mm/search?q=",
"mn": "https://www.google.mn/search?q=",
"ms": "https://www.google.ms/search?q=",
"mt": "https://www.google.com.mt/search?q=",
"mu": "https://www.google.mu/search?q=",
"mv": "https://www.google.mv/search?q=",
"mw": "https://www.google.mw/search?q=",
"mx": "https://www.google.com.mx/search?q=",
"my": "https://www.google.com.my/search?q=",
"mz": "https://www.google.co.mz/search?q=",
"na": "https://www.google.com.na/search?q=",
"ne": "https://www.google.ne/search?q=",
"nf": "https://www.google.com.nf/search?q=",
"ng": "https://www.google.com.ng/search?q=",
"ni": "https://www.google.com.ni/search?q=",
"nl": "https://www.google.nl/search?q=",
"no": "https://www.google.no/search?q=",
"np": "https://www.google.com.np/search?q=",
"nr": "https://www.google.nr/search?q=",
"nu": "https://www.google.nu/search?q=",
"nz": "https://www.google.co.nz/search?q=",
"om": "https://www.google.com.om/search?q=",
"pk": "https://www.google.com.pk/search?q=",
"pa": "https://www.google.com.pa/search?q=",
"pe": "https://www.google.com.pe/search?q=",
"ph": "https://www.google.com.ph/search?q=",
"pl": "https://www.google.pl/search?q=",
"pg": "https://www.google.com.pg/search?q=",
"pn": "https://www.google.pn/search?q=",
"pr": "https://www.google.com.pr/search?q=",
"ps": "https://www.google.ps/search?q=",
"pt": "https://www.google.pt/search?q=",
"py": "https://www.google.com.py/search?q=",
"qa": "https://www.google.com.qa/search?q=",
"ro": "https://www.google.ro/search?q=",
"rs": "https://www.google.rs/search?q=",
"ru": "https://www.google.ru/search?q=",
"rw": "https://www.google.rw/search?q=",
"sa": "https://www.google.com.sa/search?q=",
"sb": "https://www.google.com.sb/search?q=",
"sc": "https://www.google.sc/search?q=",
"se": "https://www.google.se/search?q=",
"sg": "https://www.google.com.sg/search?q=",
"sh": "https://www.google.sh/search?q=",
"si": "https://www.google.si/search?q=",
"sk": "https://www.google.sk/search?q=",
"sl": "https://www.google.com.sl/search?q=",
"sn": "https://www.google.sn/search?q=",
"sm": "https://www.google.sm/search?q=",
"so": "https://www.google.so/search?q=",
"st": "https://www.google.st/search?q=",
"sr": "https://www.google.sr/search?q=",
"sv": "https://www.google.com.sv/search?q=",
"td": "https://www.google.td/search?q=",
"tg": "https://www.google.tg/search?q=",
"th": "https://www.google.co.th/search?q=",
"tj": "https://www.google.com.tj/search?q=",
"tk": "https://www.google.tk/search?q=",
"tl": "https://www.google.tl/search?q=",
"tm": "https://www.google.tm/search?q=",
"to": "https://www.google.to/search?q=",
"tn": "https://www.google.tn/search?q=",
"tr": "https://www.google.com.tr/search?q=",
"tt": "https://www.google.tt/search?q=",
"tw": "https://www.google.com.tw/search?q=",
"tz": "https://www.google.co.tz/search?q=",
"ua": "https://www.google.com.ua/search?q=",
"ug": "https://www.google.co.ug/search?q=",
"uk": "https://www.google.co.uk/search?q=",
"us": "https://www.google.com/search?q=",
"uy": "https://www.google.com.uy/search?q=",
"uz": "https://www.google.co.uz/search?q=",
"vc": "https://www.google.com.vc/search?q=",
"ve": "https://www.google.co.ve/search?q=",
"vg": "https://www.google.vg/search?q=",
"vi": "https://www.google.co.vi/search?q=",
"vn": "https://www.google.com.vn/search?q=",
"vu": "https://www.google.vu/search?q=",
"ws": "https://www.google.ws/search?q=",
"za": "https://www.google.co.za/search?q=",
"zm": "https://www.google.co.zm/search?q=",
"zw": "https://www.google.co.zw/search?q=",
}
// UserAgents hold various browser or user agent specifications.
var UserAgents = []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38",
}
// main function calls Scrape and write data into a file.
func main() {
//var SearchTerm, CountryCode, LanguageCode string
//var Pages, Count, RestSeconds int
// [] All input data should be valid and checked here before passing to any function.
results, err := Scrape("tanmay sarkar", "com", "en", 1, 5, 5)
if err != nil {
log.Fatalln("Error spotted in main func! \t", err)
}
byteData, err := json.MarshalIndent(results, "", " ")
if err != nil {
log.Fatalln("ERROR\t", err)
}
err = os.WriteFile("./outputs/testing.json", byteData, 0644)
if err != nil {
if err.Error() == "outputs: No such file or directory" {
err = os.Mkdir("outputs", 0750)
if err != nil {
log.Fatalln("ERROR\t", err)
}
} else {
log.Fatalln("ERROR\t", err)
}
os.WriteFile("./outputs/testing.json", byteData, 0644)
}
}
// Tasks to complete for Scrape function.
// [✅] build google absolute urls for each page.
// [✅] build http client, make req, set user agent, return response.
// [✅] parse data from http.Response using goquery.
// [✅] return data in a slice of SearchResult.
// Scrape takes what to search, and it does the job and returns us the result.
func Scrape(SearchTerm, CountryCode, LanguageCode string, Pages, Count, RestSeconds int) ([]SearchResult, error) {
var finalResult []SearchResult
// Building absolute urls to scrape data.
absURLs := buildAbsUrls(SearchTerm, CountryCode, LanguageCode, Pages, Count)
// Loop through all URLs and scrape data from all of them.
for _, URL := range absURLs {
// Getting http response form of each of those abs urls.
response, err := getResponseFromClientRequest(URL)
if err != nil {
log.Fatalln("Unable to get response body.")
}
// Parsing response body into a slice of SearchResult.
data, err := parseResponse(response)
if err != nil {
log.Fatalln("Unable to parse response body.")
}
// Appending a slice into another.
finalResult = append(finalResult, data...)
}
return finalResult, nil
}
// parseResponse takes http response that using goquery it parses all necessary data and return a SearchResult object.
func parseResponse(response *http.Response) ([]SearchResult, error) {
var results []SearchResult
// Make a goquery document from that response.
document, err := goquery.NewDocumentFromResponse(response)
if err != nil {
return []SearchResult{}, err
}
//
applicableDivision := document.Find("div.g")
for index, _ := range applicableDivision.Nodes {
specificDivision := applicableDivision.Eq(index)
link, found := specificDivision.Find("a").Attr("href")
if !found {
log.Println("ERROR\tUnable to find Division Link!")
}
title := specificDivision.Find("h3.LC20lb").Text()
desc := specificDivision.Find("span").Text()
results = append(results, SearchResult{len(results) + 1, link, title, desc})
}
return results, nil
}
// getResponseFromClientRequest build http client, form a request, set user agent and returns response.
func getResponseFromClientRequest(URL string) (*http.Response, error) {
// Make http client.
client := http.DefaultClient
// Form a request.
request, err := http.NewRequest("GET", URL, nil)
if err != nil {
return nil, err
}
// Set user agent.
ua := getUserAgent()
request.Header.Set("User-Agent", ua)
// Perform request.
response, err := client.Do(request)
if err != nil {
return nil, err
}
// Return response without any error.
return response, nil
}
// buildAbsUrls build full working links by adding various info denoted from user.
func buildAbsUrls(SearchTerm, CountryCode, LanguageCode string, Pages, Count int) []string {
var AbsURLs []string
// Making SearchTerm usable in url.
SearchTerm = strings.TrimSpace(SearchTerm)
SearchTerm = strings.Replace(SearchTerm, " ", "+", -1)
// Build same quantity of URLs as the Pages.
for i := 0; i < Pages; i++ {
start := i * Count // [] Add Comment
baseURL := BaseGoogleUrls[CountryCode]
// An absolute URL would look like https://www.google.com/search?q=tanmay+sarkar&num=5&hl=en&start=1&filter=0
AbsURL := fmt.Sprintf("%s%s&num=%d&hl=%s&start=%d&filter=0", baseURL, SearchTerm, Count, LanguageCode, start)
AbsURLs = append(AbsURLs, AbsURL)
}
return AbsURLs
}
// getUserAgent returns a random agent string from UserAgents slice.
func getUserAgent() string {
rand.Seed(time.Now().Unix())
return UserAgents[rand.Int()%len(UserAgents)]
}