forked from lealife/leacrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.go
553 lines (474 loc) · 14.4 KB
/
crawler.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package leacrawler
import (
"io/ioutil"
"net/http"
"strings"
"regexp"
"log"
"os"
"path/filepath"
"github.com/lealife/leacrawler/util"
"sync"
)
type Crawler struct {
indexUrl string
scheme string // http:// 或 https://
host string // www.lealife.com lealife.com
schemeAndHost string // http://lealife.com
targetPath string
noChildrenFileExts []string
hadDoneUrl map[string] bool
exceptionUrl map[string] bool
defaultFilename string // 生成的文件名
t int
goroutineNum int // 正在运行的goroutine数目
lock *sync.Mutex
// 并发
w sync.WaitGroup
ch chan bool
}
// 实例化Crawler
func NewCrawler() *Crawler {
lea := &Crawler{
targetPath: "D:",
defaultFilename: "index.html",
t: 1,
goroutineNum: 0,
lock: &sync.Mutex{},
noChildrenFileExts: []string{".js", ".ico", ".png", ".jpg", ".gif"}}
lea.ch = make(chan bool, 1000) // 仅limit个goroutine
lea.hadDoneUrl = make(map[string]bool, 1000)
lea.exceptionUrl = make(map[string]bool, 1000)
lea.setLogOutputWriter()
return lea
}
// 入口
func (this *Crawler) Fetch(url, targetPath string) {
url = strings.TrimSpace(url)
this.parseUrl(url)
// 保存路径
this.doTargetPath(targetPath)
// 去掉scheme
// a.com, a.com/index.html
url = util.Substring(url, len(this.scheme))
// url2, ok := this.getRalativeUrl("a.com/b/c/d/kk/eee.html", "http://a.com/e/c/d/kk")
// println(url2)
// println(ok)
// return
this.goDo(url, false)
this.w.Wait()
// 处理异常
this.doExceptionUrl()
}
// go routine do it
func (this *Crawler) goDo(url string, needException bool) {
// this.do(url, false)
this.w.Add(1)
// println(">>>>>>>>>>>>申请资源" + url)
this.ch <- true // 使用资源
// println(">>>>>>>>>>>>申请资源成功" + url)
this.lock.Lock()
this.goroutineNum++
log.Println("当前共运行", this.goroutineNum, "goroutine")
this.lock.Unlock()
go func() {
defer func() {
this.w.Done()
}()
children := this.do(url, needException)
this.lock.Lock()
this.goroutineNum--
log.Println("当前共运行", this.goroutineNum, " goroutine")
this.lock.Unlock()
// println("<<<<<<<<<<<<<释放资源" + url)
<-this.ch // 释放资源
for _, cUrl := range children {
this.goDo(cUrl, false)
}
}()
}
// needException 需要处理异常?
// 这里的url可能是: a.com/b/c/d(没有schema), 不是以.html, .css, .js为后缀的
// 那么断定是一个页面, 此时自动生成一个文件名 => a.com/b/c/d/d_leaui_index.html
// 生成的文件名都按一个规则即可, 不必事先mapping
// 返回一个[]string 待处理的子
func (this *Crawler) do(url string, needException bool) (children []string) {
children = nil
url = this.trimUrl(url)
if this.isNotNeedUrl(url, needException) {
return;
}
// 文件是否已存在
// url = a.com/a/?id=12&id=1221, 那么genUrl=a.com/a/index.html?id=121
genUrl := this.genUrl(url)
if this.isExists(genUrl) {
return;
}
// 得到内容
fullUrl := this.scheme + url
if needException {
log.Println("正在处理 `异常` " + fullUrl)
} else {
log.Println("正在处理 " + fullUrl)
}
content, err := this.getContent(fullUrl)
if !needException && (err != nil || content == "") { // !needException防止处理异常时无限循环
this.exceptionUrl[url] = true
return;
}
this.hadDoneUrl[url] = true
ext := strings.ToLower(filepath.Ext(this.trimQueryParams(url))) // 很可能是a.css?v=1.3
// css文件中 url(../../img/search-icon.png)
if(ext == ".css") {
children = this.doCSS(url, content)
return;
}
// 如果是js, image文件就不往下执行了
if(util.InArray(this.noChildrenFileExts, ext)) {
// 保存该文件
if !this.writeFile(url, content) {
return;
}
return;
}
if(this.t == 1) {
// 解析html里的href, src
children = this.doHTML(url, genUrl, content)
}
return
}
// 处理css
func (this *Crawler) doCSS(url, content string) (children []string) {
children = nil
// 保存该文件
if !this.writeFile(url, content) {
return;
}
regular := "(?i)url\\((.+?)\\)"
reg := regexp.MustCompile(regular)
re := reg.FindAllStringSubmatch(content, -1)
log.Println(url + " 含有: ");
log.Println(re);
baseDir := filepath.Dir(url)
for _, each := range re {
cUrl := this.trimUrl(each[1])
// 这里, goDo会申请资源, 导致doCSS一直不能释放资源
children = append(children, this.cleanUrl(baseDir + "/" + cUrl))
}
return
}
// url : a.com/a/b/d.html
// a.com/a/b/c genFilename: c_leaui_index.html
// 生成子的相对目录有用
func (this *Crawler) doHTML(pUrl, realPUrl, content string) (children []string) {
regular := "(?i)(src=|href=)[\"']([^#].*?)[\"']"
reg := regexp.MustCompile(regular)
re := reg.FindAllStringSubmatch(content, -1)
log.Println(pUrl + " => " + realPUrl);
log.Println(pUrl + " 含有: ");
//log.Println(re);
baseDir := filepath.Dir(realPUrl)
for _, each := range re {
// 为了完整替换
// 只替换src=""里的会有子串的问题, 一个url是另一个url子串
rawFullUrl := each[0] // src='http://www.uiueux.com/wp/webzine/wp-content/themes/webzine/js/googlefont.js.php?ver=1.6.4'
rawFullUrlPrefix := each[1]; // src=
// http://a.com/, /a/b/c/d.html, /a/b.jgp
// 如果是/a/b.jpg, 那么是相对host的, 而不是本文件的路径
rawCUrl := each[2]
cUrl := rawCUrl; // strings.TrimRight(rawCUrl, "/") // 万一就是/呢?
// 如果一个链接以//开头, 那么省略了http:, 如果以/开头, 则相对于host
prefixNotHttp := false
if strings.HasPrefix(cUrl, "//") {
cUrl = this.scheme + util.Substring(cUrl, 2)
prefixNotHttp = true
} else if strings.HasPrefix(cUrl, "/") {
cUrl = this.schemeAndHost + cUrl
}
// 如果这个url是一个目录, 新建一个文件
// 如果这个url是以http://a.com开头的, host是一样的,
// 那么content的url是相对于该url
// 生成的url, 如果是目录, 会生成一个文件
cRealUrl, ok := this.getRalativeUrl(realPUrl, cUrl)
// 错误, 不是本页面, 本host的页面
if ok == -1 {
// 如果之前//替换成了http://
if prefixNotHttp {
content = strings.Replace(content, rawFullUrl, rawFullUrlPrefix + "\"" + cRealUrl + "\"", -1)
}
continue
}
// 表示已处理过, 是相对目录了, 必须把内容的替换掉
// 但要处理的还是之前的链接http://
if ok == 1 {
cRealUrl = strings.Trim(cRealUrl, "/")
// 把//变成/
for strings.Index(cRealUrl, "//") != -1 {
cRealUrl = strings.Replace(cRealUrl, "//", "/", -1)
}
log.Println(rawCUrl + " >>>>>> " + cRealUrl)
content = strings.Replace(content, rawFullUrl, rawFullUrlPrefix + "\"" + cRealUrl + "\"", -1)
cUrl = strings.Replace(cUrl, this.scheme, "", 1) // 把sheme去掉, do
children = append(children, cUrl) // 不需要clean
} else {
children = append(children, this.cleanUrl(baseDir + "/" + cRealUrl))
}
}
// 把content保存起来
if !this.writeFile(realPUrl, content) {
return;
}
// this.t++
// return
return
}
// 得到相对目录
// realPUrl: a.com/b/c/index.html 不是a.com/b/c
// cUrl如果是以this.scheme + this.host开头, 则需要转换成相对目录
// cUrl a.com/c/d/e/g
// 在realPUrl页面到cUrl跳转
func (this *Crawler) getRalativeUrl(realPUrl, cUrl string) (url string, ok int) {
ok = 0
url = cUrl
if strings.HasPrefix(cUrl, this.scheme + this.host) {
url = ""
ok = 1
realCUrl := this.genUrl(cUrl) // 如果是目录, 生成一个
// 如果realPUrl == realCurl 那么返回"#"
realPUrl = strings.Replace(realPUrl, this.host, "", 1) // 去掉a.com
realCUrl = strings.Replace(realCUrl, this.scheme + this.host, "", 1) // 去掉http://a.com
realPUrl = this.trimUrl(realPUrl)
realCUrl = this.trimUrl(realCUrl)
if realPUrl == realCUrl {
url = "#"
return
}
// 去掉两个url相同的部分
realPUrlArr := strings.Split(realPUrl, "/")
realCUrlArr := strings.Split(realCUrl, "/")
log.Println(realPUrlArr)
log.Println(realCUrlArr)
i, j := 0, 0
for ; i < len(realCUrlArr) && j < len(realPUrlArr) && realCUrlArr[i] == realPUrlArr[j]; {
realCUrlArr[i] = ""
i++
j++
}
// 有多个少../?
n := len(realPUrlArr) - i - 1
for k := 0; k < n; k++ {
url += "../"
}
url += strings.Join(realCUrlArr, "/")
return;
}
// 如果是以http://, https://开头的, 返回false
if strings.HasPrefix(cUrl, "http://") || strings.HasPrefix(cUrl, "https://") {
ok = -1
return
}
return
}
// trimSpace, /, \, ", '
func (this *Crawler) trimUrl(url string) string {
if(url != "") {
url = strings.TrimSpace(url)
url = strings.Trim(url, "\"")
url = strings.Trim(url, "'")
url = strings.Trim(url, "/")
url = strings.Trim(url, "\\")
}
return url
}
// 处理异常
func (this *Crawler) doExceptionUrl() {
if(len(this.exceptionUrl) > 0) {
log.Println("正在处理异常Url....");
for url, _ := range this.exceptionUrl {
this.do(url, true)
}
}
}
// 如果url是 a.com/b/c/d
// 生成一个文件a.com/b/c/d/d_leaui_index.html
// 返回 d_leaui_index.html
// 如果不是一个目录, 返回""
func (this *Crawler) genFilename(url string) (string, bool) {
urlArr := strings.Split(url, "/")
if urlArr != nil {
last := urlArr[len(urlArr) - 1]
ext := strings.ToLower(filepath.Ext(last))
if ext == "" {
return this.defaultFilename, true // 需要append到url后面
} else if util.InArray([]string{".php", ".jsp", ".asp", ".aspx"}, ext) {
filename := filepath.Base(last) // a.php
filename = util.Substr(filename, 0, len(filename) - len(ext)) // a
return filename + ".html", false
}
}
return "", true;
}
// 生成真实的url
// 传来的url可能是http://a.com, 也可能是a.com
// getRelativeUrl传来的可以是http://a.com
// url = a.com/a/?id=12&id=1221, 那么genUrl=a.com/a/index.html?id=121
func (this *Crawler) genUrl(url string) string {
// 去掉?后面的
queryParam, fragment := "", "" // 包含?,#
pos := strings.Index(url, "?");
if pos != -1 {
queryParam = util.Substring(url, pos)
url = util.Substr(url, 0, pos);
} else {
pos = strings.Index(url, "#");
if pos != -1 {
fragment = util.Substring(url, pos)
url = util.Substr(url, 0, pos);
}
}
// 如果url == host
if url == this.host || url == this.schemeAndHost {
return url + "/" + this.defaultFilename + queryParam + fragment
}
genFilename, needApend := this.genFilename(url)
if genFilename != "" {
if needApend {
url += "/" + genFilename + queryParam + fragment
} else {
// 是a.php => a.html
urlArr := strings.Split(url, "/")
urlArr = urlArr[:len(urlArr)-1]
url = strings.Join(urlArr, "/") + "/" + genFilename
}
}
return url
}
func (this *Crawler) writeFile(url, content string) bool {
// $path = a.html?a=a11
url = this.trimQueryParams(url)
fullPath := this.targetPath + "/" + url
dir := filepath.Dir(fullPath)
log.Println("写目录", dir);
if err := os.MkdirAll(dir, 0777); err != nil {
log.Println("写目录" + dir + " 失败")
return false
}
// 写到文件中
file, err := os.Create(fullPath)
defer file.Close()
if err != nil {
log.Println("写文件" + fullPath + " 失败")
return false
}
file.WriteString(content)
return true;
}
func (this *Crawler) cleanUrl(url string) string {
url = filepath.Clean(url)
return strings.Replace(url, "\\", "/", -1)
}
// 将url ?, #后面的字符串去掉
func (this *Crawler) trimQueryParams(url string) string {
pos := strings.Index(url, "?");
if pos != -1 {
url = util.Substr(url, 0, pos);
}
pos = strings.Index(url, "#");
if pos != -1 {
url = util.Substr(url, 0, pos);
}
return url;
}
// 判断是否已存在
// url = a/b/c/d.html
func (this *Crawler) isExists(url string) bool {
return util.IsExists(this.targetPath + "/" + url)
}
// 不需要处理的url
// needException false 表示不要处理, 那么就要判断是否在其中
func (this *Crawler) isNotNeedUrl(url string, needException bool) bool {
if _, ok := this.hadDoneUrl[url]; ok {
return true
}
_, ok := this.exceptionUrl[url];
if !needException && ok {
return true
}
// http:\\/|https:\\/|
regular := "#|javascript:|mailto:|" class=|@.*?\\..+"
reg := regexp.MustCompile(regular)
if reg.MatchString(url) {
return true
}
if (strings.HasPrefix(url, "http:/") || strings.HasPrefix(url, "https:/")) &&
!strings.HasPrefix(url, this.scheme + this.host) {
return true
}
return false
}
// 处理url, 得到scheme, host
func (this *Crawler) parseUrl(url string) {
if(strings.HasPrefix(url, "http://")) {
this.scheme = "http://";
} else {
this.scheme = "https://";
}
// http://lealife.com/b/c
url = strings.Replace(url, this.scheme, "", 1)
index := strings.Index(url, "/")
if(index == -1) {
this.host = url
} else {
this.host = util.Substr(url, 0, index)
}
this.schemeAndHost = this.scheme + this.host
}
func (this *Crawler) getNoChildrenFileExts() []string {
return this.noChildrenFileExts;
}
// 得到内容
func (this *Crawler) getContent(url string) (content string, err error) {
var resp *http.Response
resp, err = http.Get(url)
if(resp != nil && resp.Body != nil) {
defer resp.Body.Close()
} else {
log.Println("ERROR " + url + " 返回为空 ")
}
if resp == nil || resp.Body == nil || err != nil || resp.StatusCode != http.StatusOK {
log.Println("ERROR " + url)
log.Println(err)
return
}
var buf []byte
buf, err = ioutil.ReadAll(resp.Body)
if(err != nil) {
return
}
content = string(buf);
return
}
// 生成存储位置
func (this *Crawler) doTargetPath(path string) {
path = strings.TrimRight(path, "/"); // 不能TrimLeft, 万一是linux呢?
path = strings.Trim(path, "\\");
if path != "" {
this.targetPath = path;
}
// 生成目录
if this.targetPath != "" {
os.MkdirAll(this.targetPath, 0777)
} else {
panic("存储位置异常")
}
}
func (this *Crawler) setLogOutputWriter() {
/*
logfile, err := os.OpenFile("C:/Users/Administrator/workspace/lea/log.txt", os.O_RDWR|os.O_CREATE, 0);
if err != nil {
log.Printf("%s\r\n", err.Error());
os.Exit(-1);
}
log.SetOutput(logfile)
*/
}