forked from yhat/scrape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.go
More file actions
131 lines (117 loc) · 3.25 KB
/
Copy pathscrape.go
File metadata and controls
131 lines (117 loc) · 3.25 KB
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
// 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.
func FindAll(node *html.Node, matcher Matcher) []*html.Node {
if matcher(node) {
return []*html.Node{node}
}
matched := []*html.Node{}
for c := node.FirstChild; c != nil; c = c.NextSibling {
found := FindAll(c, matcher)
if len(found) > 0 {
matched = append(matched, found...)
}
}
return matched
}
// 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
}
}