/
work5_8.go
74 lines (65 loc) · 1.35 KB
/
work5_8.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
package main
import (
"fmt"
"net/http"
"os"
"golang.org/x/net/html"
)
// run command
// go run ./src/chapter5/work5_8/work5_8.go https://golang.org/ nav
func main() {
url := os.Args[1]
targetID := os.Args[2]
if url == "" || targetID == "" {
fmt.Fprintf(os.Stderr, "need params url and targetId")
}
doc, err := getDoc(url)
if err != nil {
fmt.Fprintf(os.Stderr, "err: %v\n", err)
os.Exit(1)
}
foundNode := elementByID(doc, targetID)
fmt.Printf("foundNode: %v\n", foundNode)
}
var foundNode *html.Node
func elementByID(doc *html.Node, id string) *html.Node {
forEachNode(doc, id, findID, nil)
return foundNode
}
func getDoc(url string) (*html.Node, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
doc, err := html.Parse(resp.Body)
resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("parsing HTML: %s", err)
}
return doc, nil
}
func forEachNode(n *html.Node, id string, pre, post func(n *html.Node, id string) bool) {
if pre != nil {
goOn := pre(n, id)
if !goOn {
return
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
forEachNode(c, id, pre, post)
}
if post != nil {
post(n, id)
}
}
func findID(n *html.Node, id string) bool {
if n.Type == html.ElementNode {
for _, a := range n.Attr {
if a.Key == "id" && a.Val == id {
foundNode = n
return false
}
}
}
return true
}