| 1 |
// Package main ... |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/go-rod/rod" |
| 14 |
"github.com/go-rod/rod/lib/proto" |
| 15 |
) |
| 16 |
|
| 17 |
func main() { |
| 18 |
// create a test server to serve the page |
| 19 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 20 |
_, _ = fmt.Fprint(w, ` |
| 21 |
<html lang="en"> |
| 22 |
<head> |
| 23 |
<meta charset="UTF-8"> |
| 24 |
<title>Title</title> |
| 25 |
</head> |
| 26 |
<body> |
| 27 |
<h1 id="title" class="link"> |
| 28 |
<a href="https://test.com/helloworld"> |
| 29 |
content of h1 1 |
| 30 |
</a> |
| 31 |
<span>hello</span> world |
| 32 |
</h1> |
| 33 |
</body> |
| 34 |
</html> |
| 35 |
`, |
| 36 |
) |
| 37 |
})) |
| 38 |
defer ts.Close() |
| 39 |
|
| 40 |
page := rod.New().MustConnect().MustPage(ts.URL) |
| 41 |
|
| 42 |
node, err := page.MustElement("body").Describe(-1, true) |
| 43 |
if err != nil { |
| 44 |
log.Fatal(err) |
| 45 |
} |
| 46 |
|
| 47 |
printNodes(os.Stdout, node.Children, "", " ") |
| 48 |
} |
| 49 |
|
| 50 |
func printNodes(w io.Writer, nodes []*proto.DOMNode, padding, indent string) { |
| 51 |
for _, node := range nodes { |
| 52 |
switch { |
| 53 |
case node.NodeName == "#text": |
| 54 |
fmt.Fprintf(w, "%s#text: %q\n", padding, node.NodeValue) |
| 55 |
default: |
| 56 |
fmt.Fprintf(w, "%s%s:\n", padding, strings.ToLower(node.NodeName)) |
| 57 |
if n := len(node.Attributes); n > 0 { |
| 58 |
fmt.Fprintf(w, "%sattributes:\n", padding+indent) |
| 59 |
for i := 0; i < n; i += 2 { |
| 60 |
fmt.Fprintf(w, "%s%s: %q\n", padding+indent+indent, node.Attributes[i], node.Attributes[i+1]) |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
if node.ChildNodeCount != nil { |
| 65 |
fmt.Fprintf(w, "%schildren:\n", padding+indent) |
| 66 |
printNodes(w, node.Children, padding+indent+indent, indent) |
| 67 |
} |
| 68 |
} |
| 69 |
} |