A golang package, lets you extract data from HTML/XML documents using XPath expression.
List of supported XPath functions you can found at XPath Package.
go get github.com/antchfx/xquery
Extract data from HTML document.
package main
import (
"github.com/antchfx/xpath"
"github.com/antchfx/xquery/html"
)
func main() {
f, _ := os.Open(`./examples/test.html`)
doc, _ := htmlquery.Parse(f)
expr := xpath.MustCompile("count(//div[@class='article'])")
fmt.Printf("%f \n", expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(float64))
expr = xpath.MustCompile("//a/@href")
iter := expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(*xpath.NodeIterator)
for iter.MoveNext() {
fmt.Printf("%s \n", iter.Current().Value()) // output href
}
for _, n := range htmlquery.Find(doc, "//a/@href") {
fmt.Printf("%s \n", htmlquery.SelectAttr(n, "href")) // output href
}
}
Extract data from XML document.
package main
import (
"github.com/antchfx/xpath"
"github.com/antchfx/xquery/xml"
)
func main() {
f, _ := os.Open(`./examples/test.xml`)
doc, _ := xmlquery.Parse(f)
// sum all book's price via Evaluate()
expr, err := xpath.Compile("sum(//book/price)")
if err != nil {
panic(err)
}
fmt.Printf("total price: %f\n", expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64))
for _, n := range xmlquery.Find(doc, "//book") {
fmt.Printf("%s : %s \n", n.SelectAttr("id"), xmlquery.FindOne(n, "title").InnerText())
}
n := xmlquery.FindOne(doc, "//book[@id='bk104']")
fmt.Printf("%s \n", n.OutputXML(true))
}