go html选择器

html.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
package h0

import (
"net/http"
"strings"

"golang.org/x/net/html"
)

type Node html.Node

func (node *Node) Select(path string, filter func(*html.Node) bool) []*Node {
if path == "" {
return nil
}
var nodes []*Node
if path[0] == '/' {
if node.Parent != nil {
return ((*Node)(node.Parent)).Select(path, filter)
} else {
path = path[1:]
}
}
p := strings.IndexByte(path, '/')
var label string
if p >= 0 {
label = path[:p]
path = path[p+1:]
} else {
label = path
path = ""
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
if label == "*" || c.Data == label {
if path != "" {
nodes = append(nodes, ((*Node)(c)).Select(path, filter)...)
} else {
if filter != nil && !filter(c) {
continue
}
nodes = append(nodes, ((*Node)(c)))
}
}
}
return nodes
}

func (node *Node) GetAttr(key string) string {
for _, a := range node.Attr {
if a.Key == key {
return a.Val
}
}
return ""
}

func parseHtml(resp *http.Response) (*Node, error) {
defer resp.Body.Close()
n, err := html.Parse(resp.Body)
if err != nil {
return nil, err
}
return (*Node)(n), nil
}