htmlquery is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression.
$ go get github.com/antchfx/htmlquery
doc, err := htmlquery.LoadURL("http://example.com/")s := `<html>....</html>`
doc, err := htmlquery.Parse(strings.NewReader(s))list := htmlquery.Find(doc, "//a")list := range htmlquery.Find(doc, "//a/@href") a := htmlquery.FindOne(doc, "//a[3]")expr, _ := xpath.Compile("count(//img)")
v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(float64)
fmt.Printf("total count is %f", v)func main() {
doc, err := htmlquery.LoadURL("https://www.bing.com/search?q=golang")
if err != nil {
panic(err)
}
// Find all news item.
for i, n := range htmlquery.Find(doc, "//ol/li") {
a := htmlquery.FindOne(n, "//a")
fmt.Printf("%d %s(%s)\n", i, htmlquery.InnerText(a), htmlquery.SelectAttr(a, "href"))
}
}| Name | Description |
|---|---|
| htmlquery | XPath query package for the HTML document |
| xmlquery | XPath query package for the XML document |
| jsonquery | XPath query package for the JSON document |
If you have any questions, create an issue and welcome to contribute.