-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (103 loc) · 2.58 KB
/
main.go
File metadata and controls
125 lines (103 loc) · 2.58 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
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/c-bata/go-prompt"
"github.com/tebeka/selenium"
)
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func main() {
fmt.Println("Crwaler v1")
const (
// These paths will be different on your system.
seleniumPath = "bin/selenium-server.jar"
geckoDriverPath = "bin/geckodriver"
port = 8080
)
querys := strings.Split(prompt.Input("> ", completer), ",")
opts := []selenium.ServiceOption{
selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox
}
// selenium.SetDebug(true)
service, err := selenium.NewSeleniumService(seleniumPath, port, opts...)
if err != nil {
panic(err) // panic is used only as an example and is not otherwise recommended.
}
defer service.Stop()
// Connect to the WebDriver instance running locally.
caps := selenium.Capabilities{"browserName": "firefox"}
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil {
panic(err)
}
defer wd.Quit()
for _, query := range querys {
os.MkdirAll("./downloads/"+query, os.ModeDir)
// Navigate to the simple playground interface.
if err := wd.Get("http://google.com/search?tbm=isch&q=" + query); err != nil {
panic(err)
}
i := -1
str := ""
for true {
if i > 130 {
break
}
elems, err := wd.FindElements(selenium.ByCSSSelector, ".rg_i")
if err != nil {
panic(err)
}
for i2, elem := range elems {
if i >= i2 {
continue
} else {
i = i2
}
elem.Click()
elem2, err := wd.FindElement(selenium.ByCSSSelector, ".n3VNCb")
if err != nil {
panic(err)
}
src, err := elem2.GetAttribute("src")
if err != nil {
panic(err)
}
if strings.HasPrefix(src, "https://encrypted-tbn0.gstatic.com/") {
if str == src {
continue
} else {
str = src
}
filepath := fmt.Sprintf("./downloads/%s/%d", query, i2)
fmt.Printf("saved \"%s\" to \"%s\"\n", src, filepath)
downloadFile(filepath, src)
}
}
}
}
}
func downloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
contype := strings.Split(resp.Header.Get("content-type"), "/")[1]
filepath = filepath + "." + contype
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}