...
1
2
3
4
5
6 package html_test
7
8 import (
9 "fmt"
10 "log"
11 "strings"
12
13 "golang.org/x/net/html"
14 )
15
16 func ExampleParse() {
17 s := `<p>Links:</p><ul><li><a href="foo">Foo</a><li><a href="/bar/baz">BarBaz</a></ul>`
18 doc, err := html.Parse(strings.NewReader(s))
19 if err != nil {
20 log.Fatal(err)
21 }
22 var f func(*html.Node)
23 f = func(n *html.Node) {
24 if n.Type == html.ElementNode && n.Data == "a" {
25 for _, a := range n.Attr {
26 if a.Key == "href" {
27 fmt.Println(a.Val)
28 break
29 }
30 }
31 }
32 for c := n.FirstChild; c != nil; c = c.NextSibling {
33 f(c)
34 }
35 }
36 f(doc)
37
38
39
40 }
41
View as plain text