-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrawl.go
59 lines (48 loc) · 939 Bytes
/
crawl.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
package main
import (
"flag"
"fmt"
"os"
"strings"
colly "github.com/gocolly/colly/v2"
)
func onHtml(e *colly.HTMLElement) {
link := e.Attr("href")
e.Request.Visit(link)
src := e.Attr("src")
e.Request.Visit(src)
act := e.Attr("action")
e.Request.Visit(act)
}
func onRequest(r *colly.Request) {
fmt.Println(r.URL)
}
func main() {
url := flag.String("url", "", "url to crawl")
cache := flag.Bool("cache", false, "enable cache")
flag.Parse()
if *url == "" {
fmt.Println("select a domain to crawl -url or -h")
os.Exit(1)
}
purl := strings.Split(*url, "/")
if len(purl) < 3 {
fmt.Println("bad url")
os.Exit(1)
}
dom := purl[2]
var c *colly.Collector
if *cache {
c = colly.NewCollector(
colly.AllowedDomains(dom),
colly.CacheDir("./crawler_cache"),
)
} else {
c = colly.NewCollector(
colly.AllowedDomains(dom),
)
}
c.OnHTML("a[href]", onHtml)
c.OnRequest(onRequest)
c.Visit(*url)
}