forked from yhat/scrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.go
189 lines (168 loc) · 4.99 KB
/
scrape.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
// Package scrape provides a searching api on top of golang.org/x/net/html
package scrape
import (
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Matcher should return true when a desired node is found.
type Matcher func(node *html.Node) bool
// FindAll returns all nodes which match the provided Matcher. After discovering a matching
// node, it will _not_ discover matching subnodes of that node.
func FindAll(node *html.Node, matcher Matcher) []*html.Node {
return findAllInternal(node, matcher, false)
}
// FindAllNested returns all nodes which match the provided Matcher and _will_ discover
// matching subnodes of matching nodes.
func FindAllNested(node *html.Node, matcher Matcher) []*html.Node {
return findAllInternal(node, matcher, true)
}
// Find returns the first node which matches the matcher using depth-first search.
// If no node is found, ok will be false.
//
// root, err := html.Parse(resp.Body)
// if err != nil {
// // handle error
// }
// matcher := func(n *html.Node) bool {
// return n.DataAtom == atom.Body
// }
// body, ok := scrape.Find(root, matcher)
func Find(node *html.Node, matcher Matcher) (n *html.Node, ok bool) {
if matcher(node) {
return node, true
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
n, ok := Find(c, matcher)
if ok {
return n, true
}
}
return nil, false
}
// FindParent searches up HTML tree from the current node until either a
// match is found or the top is hit.
func FindParent(node *html.Node, matcher Matcher) (n *html.Node, ok bool) {
for p := node.Parent; p != nil; p = p.Parent {
if matcher(p) {
return p, true
}
}
return nil, false
}
// Text returns text from all descendant text nodes joined.
// For control over the join function, see TextJoin.
func Text(node *html.Node) string {
joiner := func(s []string) string {
n := 0
for i := range s {
trimmed := strings.TrimSpace(s[i])
if trimmed != "" {
s[n] = trimmed
n++
}
}
return strings.Join(s[:n], " ")
}
return TextJoin(node, joiner)
}
// TextJoin returns a string from all descendant text nodes joined by a
// caller provided join function.
func TextJoin(node *html.Node, join func([]string) string) string {
nodes := FindAll(node, func(n *html.Node) bool { return n.Type == html.TextNode })
parts := make([]string, len(nodes))
for i, n := range nodes {
parts[i] = n.Data
}
return join(parts)
}
// Attr returns the value of an HTML attribute.
func Attr(node *html.Node, key string) string {
for _, a := range node.Attr {
if a.Key == key {
return a.Val
}
}
return ""
}
// ByTag returns a Matcher which matches all nodes of the provided tag type.
//
// root, err := html.Parse(resp.Body)
// if err != nil {
// // handle error
// }
// title, ok := scrape.Find(root, scrape.ByTag(atom.Title))
func ByTag(a atom.Atom) Matcher {
return func(node *html.Node) bool { return node.DataAtom == a }
}
// ById returns a Matcher which matches all nodes with the provided id.
func ById(id string) Matcher {
return func(node *html.Node) bool { return Attr(node, "id") == id }
}
// ByClass returns a Matcher which matches all nodes with the provided class.
func ByClass(class string) Matcher {
return func(node *html.Node) bool {
classes := strings.Fields(Attr(node, "class"))
for _, c := range classes {
if c == class {
return true
}
}
return false
}
}
// findAllInternal encapsulates the node tree traversal
func findAllInternal(node *html.Node, matcher Matcher, searchNested bool) []*html.Node {
matched := []*html.Node{}
if matcher(node) {
matched = append(matched, node)
if !searchNested {
return matched
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
found := findAllInternal(c, matcher, searchNested)
if len(found) > 0 {
matched = append(matched, found...)
}
}
return matched
}
// Find returns the first node which matches the matcher using next sibling search.
// If no node is found, ok will be false.
//
// root, err := html.Parse(resp.Body)
// if err != nil {
// // handle error
// }
// matcher := func(n *html.Node) bool {
// return n.DataAtom == atom.Body
// }
// body, ok := scrape.FindNextSibling(root, matcher)
func FindNextSibling(node *html.Node, matcher Matcher) (n *html.Node, ok bool) {
for s := node.NextSibling; s != nil; s = s.NextSibling {
if matcher(s) {
return s, true
}
}
return nil, false
}
// Find returns the first node which matches the matcher using previous sibling search.
// If no node is found, ok will be false.
//
// root, err := html.Parse(resp.Body)
// if err != nil {
// // handle error
// }
// matcher := func(n *html.Node) bool {
// return n.DataAtom == atom.Body
// }
// body, ok := scrape.FindPrevSibling(root, matcher)
func FindPrevSibling(node *html.Node, matcher Matcher) (n *html.Node, ok bool) {
for s := node.PrevSibling; s != nil; s = s.PrevSibling {
if matcher(s) {
return s, true
}
}
return nil, false
}