-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectLinks.go
172 lines (161 loc) · 4.01 KB
/
collectLinks.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
package GoLib
import (
"github.com/anaskhan96/soup"
"golang.org/x/net/html"
"io"
"strconv"
"strings"
)
// All takes a reader object (like the one returned from http.Get())
// It returns a slice of strings representing the "href" attributes from
// anchor links found in the provided html.
// It does not close the reader passed to it.
type CollectLinksResult struct {
Links []string
Js []string
Css []string
}
//去除重复字符串
func RemoveRepeatedElement(arr []string) (newArr []string) {
newArr = make([]string, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat && arr[i] != "" {
newArr = append(newArr, arr[i])
}
}
return newArr
}
//对爬取到的链接进行简单处理
//打算后期加点模板对链接去重
func dealLinks(links []string) []string {
var result []string
for _, link := range links {
if link == "/" || link == "//" || strings.HasPrefix(link, "javascript") || strings.TrimSpace(link) == "" {
continue
}
if strings.HasPrefix(link, "//") {
link = "http:" + link
}
result = append(result, link)
}
return RemoveRepeatedElement(result)
}
func GetAllLinks(content string) CollectLinksResult {
var links []string
var js []string
var css []string
doc := soup.HTMLParse(content)
//获取a href
aLinks := doc.FindAll("a")
for _, b := range aLinks {
links = append(links, b.Attrs()["href"])
}
aLinks = nil
//script src
jsLinks := doc.FindAll("script")
for _, b := range jsLinks {
js = append(js, b.Attrs()["src"])
}
jsLinks = nil
//link href
Links := doc.FindAll("link")
for _, b := range Links {
css = append(css, b.Attrs()["href"])
}
Links = nil
return CollectLinksResult{Links: dealLinks(links), Js: dealLinks(js), Css: dealLinks(css)}
}
func All(httpBody io.Reader) CollectLinksResult {
var links []string
var col []string
var jsCol []string
var js []string
var css []string
var cssCol []string
page := html.NewTokenizer(httpBody)
for {
tokenType := page.Next()
if tokenType == html.ErrorToken {
return CollectLinksResult{Links: dealLinks(links), Js: dealLinks(js), Css: dealLinks(css)}
}
token := page.Token()
//a标签的href属性添加,最后需要去除一些无用链接以及添加一个路径到链接里面去
if tokenType == html.StartTagToken && token.DataAtom.String() == "a" {
for _, attr := range token.Attr {
if attr.Key == "href" {
tl := trimHash(attr.Val)
col = append(col, tl)
resolv(&links, col)
}
}
} else if tokenType == html.StartTagToken && token.DataAtom.String() == "form" {
//form标签的action属性添加
for _, attr := range token.Attr {
if attr.Key == "action" {
tl := trimHash(attr.Val)
col = append(col, tl)
resolv(&links, col)
}
}
} else if tokenType == html.StartTagToken && token.DataAtom.String() == "script" {
//script的src标签添加
for _, attr := range token.Attr {
if attr.Key == "src" {
tl := trimHash(attr.Val)
jsCol = append(jsCol, tl)
resolv(&js, jsCol)
}
}
} else if tokenType == html.StartTagToken && token.DataAtom.String() == "link" {
//link href
for _, attr := range token.Attr {
if attr.Key == "href" {
tl := trimHash(attr.Val)
cssCol = append(cssCol, tl)
resolv(&css, cssCol)
}
}
}
}
}
// trimHash slices a hash # from the link
func trimHash(l string) string {
if strings.Contains(l, "#") {
var index int
for n, str := range l {
if strconv.QuoteRune(str) == "'#'" {
index = n
break
}
}
return l[:index]
}
return l
}
// check looks to see if a url exits in the slice.
func check(sl []string, s string) bool {
var check bool
for _, str := range sl {
if str == s {
check = true
break
}
}
return check
}
// resolv adds links to the link slice and insures that there is no repetition
// in our collection.
func resolv(sl *[]string, ml []string) {
for _, str := range ml {
if check(*sl, str) == false {
*sl = append(*sl, str)
}
}
}