-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
93 lines (77 loc) · 2.1 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
package main
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
)
type Crawler struct {
WaitGroup *sync.WaitGroup
StartURL string
Levels int
Crawled int
Documents []Document
}
func NewCrawler(startURL string, levels int) *Crawler {
return &Crawler{
WaitGroup: new(sync.WaitGroup),
StartURL: startURL,
Levels: levels,
Crawled: 0,
Documents: make([]Document, 0),
}
}
func (c *Crawler) Crawle() {
rootDocument := NewDocument(c.StartURL, 1)
c.WaitGroup.Add(1)
c.CrawleDocument(rootDocument)
c.WaitGroup.Wait()
}
func (c *Crawler) ParseTitle(doc *goquery.Document, document *Document) {
document.Title = doc.Find("title").Text()
}
func (c *Crawler) ParseH1(doc *goquery.Document, document *Document) {
document.H1 = doc.Find("h1").Text()
}
func (c *Crawler) ParseBody(doc *goquery.Document, document *Document) {
body, err := doc.Find("body").Html()
if err != nil {
logError(fmt.Sprintf(fmt.Sprintf("Failed (Lvl %v): %s", document.Level, document.URL)))
return
}
document.Body = body
}
func (c *Crawler) ParseAnchors(doc *goquery.Document, document *Document) {
doc.Find("a").Each(func(i int, anchor *goquery.Selection) {
val, exists := anchor.Attr("href")
if exists {
document.URLs = append(document.URLs, val)
c.WaitGroup.Add(1)
go c.CrawleDocument(NewDocument(val, document.Level+1))
}
})
}
func (c *Crawler) CrawleDocument(document Document) {
defer c.WaitGroup.Done()
if document.Level <= c.Levels && strings.Index(document.URL, "http") == 0 {
res, err := http.Get(document.URL)
if err != nil {
logError(fmt.Sprintf("Failed (Lvl %v): %s", document.Level, document.URL))
return
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
logError(fmt.Sprintf("Failed (Lvl %v): %s", document.Level, document.URL))
return
}
c.ParseTitle(doc, &document)
c.ParseH1(doc, &document)
c.ParseBody(doc, &document)
c.ParseAnchors(doc, &document)
c.Documents = append(c.Documents, document)
c.Crawled++
logSuccess(fmt.Sprintf("Success (Lvl %v): %s", document.Level, document.URL))
}
}