-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.go
205 lines (172 loc) · 4.54 KB
/
spider.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
package main
import (
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"net/http"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
type MyRoundTripper struct {
rt http.RoundTripper
}
func (mrt MyRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36")
return mrt.rt.RoundTrip(r)
}
func GetMatches(url string, expr string) [][]string {
client := &http.Client{
Timeout: time.Second * 10,
Transport: MyRoundTripper{rt: http.DefaultTransport},
}
resp, err := client.Get(url)
if err != nil {
fmt.Printf("Get %s failed, err: %s\n", url, err)
return nil
}
defer func() {
if e := resp.Body.Close(); e != nil {
fmt.Printf("body close failed,err: %s\n", e)
return
}
}()
bytes, _ := ioutil.ReadAll(resp.Body)
compile, _ := regexp.Compile(expr)
matches := compile.FindAllStringSubmatch(string(bytes), -1)
return matches
}
func GetDetailUrl(url string) []string {
var detailUrls []string
matches := GetMatches(url, `</span>(.{0,5})<a href="(.*?)"><span>`)
for _, value := range matches {
detailUrls = append(detailUrls, value[2])
}
detailUrls = append(detailUrls, url)
return detailUrls
}
func ParseHomePage(baseUrl string, pageNum int, detailUrlChan chan []string) []string {
var detailUrls []string
for i := 1; i <= pageNum; i++ {
go func(num int) {
url := baseUrl + strconv.Itoa(num) + "/"
fmt.Printf("开始解析第%d页\n", num)
matches := GetMatches(url, `<h2><a href="(.*?)"`)
for _, value := range matches {
detailUrls = append(detailUrls, GetDetailUrl(value[1])...)
}
fmt.Printf("解析第%d页完成\n", num)
detailUrlChan <- detailUrls
}(i)
}
return detailUrls
}
func ParsePageDetail(url string, gifChan chan map[string]string) {
matches := GetMatches(url, `<p><img(.*?)src="(.*?)"\s+alt="(.*?)"`)
for _, value := range matches {
if len(value[2]) == 0 {
fmt.Printf("%s 获取gifurl失败\n", url)
} else {
gifChan <- map[string]string{"url": value[2], "title": value[3]}
}
}
}
func DownloadGif(gifInfo map[string]string, wg *sync.WaitGroup, filePath string) {
fmt.Printf("start download %s\n", gifInfo["url"])
resp, err := http.Get(gifInfo["url"])
if err != nil {
fmt.Printf("Get %s failed,err: %s", gifInfo["url"], err)
return
}
defer func() {
if e := resp.Body.Close(); e != nil {
fmt.Printf("body close failed,err: %s", e)
return
}
}()
bytes, _ := ioutil.ReadAll(resp.Body)
var fileName string
title := strings.Replace(gifInfo["title"], "https://", "", -1)
if strings.HasSuffix(gifInfo["title"], ".gif") {
fileName = filePath + title
} else {
fileName = filePath + title + ".gif"
}
if err := ioutil.WriteFile(fileName, bytes, 0664); err != nil {
fmt.Printf("download %s failed, err: %s", gifInfo["url"], err)
return
}
fmt.Printf("download %s successful\n", gifInfo["url"])
wg.Done()
}
func Spider(num int, page int, path string) {
wg := new(sync.WaitGroup)
detailUrlChan := make(chan []string)
gifChan := make(chan map[string]string, num)
baseUrl := "https://www.8mfh.com/gifchuchu/page/"
go ParseHomePage(baseUrl, page, detailUrlChan)
if runtime.GOOS == "windows" && !strings.HasSuffix(path, `\`) {
path = path + `\`
} else if runtime.GOOS == "linux" && !strings.HasSuffix(path, `/`) {
path = path + `/`
}
_, e := os.Stat(path)
if e != nil {
if e := os.MkdirAll(path, os.ModeDir); e != nil {
panic("创建目录" + path + "失败!")
}
}
for i := 0; i < page; i++ {
urls := <-detailUrlChan
for _, url := range urls {
go ParsePageDetail(url, gifChan)
}
wg.Add(len(urls))
for i := 0; i < len(urls); i++ {
gifInfo := <-gifChan
go DownloadGif(gifInfo, wg, path)
}
}
wg.Wait()
}
func main() {
var path string
if runtime.GOOS == "windows" {
path = `D:\gifs\`
} else {
path = `/tmp/gifs/`
}
app := cli.NewApp()
app.Version = "1.0.0"
app.Usage = "爬取某动图网站的出处动图"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "num,n",
Value: 10,
Usage: "设置默认下载并发数量,默认是10",
},
cli.IntFlag{
Name: "page,p",
Value: 2,
Usage: "设置下载的页数,默认是2",
},
cli.StringFlag{
Name: "to",
Value: path,
Usage: "设置下载路径,windows默认路径是D:\\gifs,其他系统默认是/tmp/gifs `PATH`",
},
}
app.Action = func(c *cli.Context) error {
Spider(c.Int("num"), c.Int("page"), c.String("to"))
return nil
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
return
}
}