Use golang to quickly get html node data
|
||
---|---|---|
.gitignore | ||
go.mod | ||
go.sum | ||
goh_test.go | ||
goh.go | ||
README.md |
goh
use golang to parse html and get node values
Usage
package main
import (
"fmt"
"strings"
"gitter.top/common/goh"
)
func main() {
reader := strings.NewReader(`<!DOCTYPE html><html lang="en"><head></head><body><h1>hello world</h1><div class="foo"><div id="bar">bar</div></div></body></html>`)
parser, err := goh.NewParser(reader)
if err != nil {
panic(err)
}
h1Value, err := parser.Find("h1").Value()
if err != nil {
panic(err)
}
fmt.Println("h1 value:", h1Value) // hello world
barValue, err := parser.Find("div.foo #bar").Value()
if err != nil {
panic(err)
}
fmt.Println("bar value:", barValue) // bar
}