XQuery 文档数据提取 Golang 包 项目简介
XQuery 是一个 Golang 包,允许从 HTML 或 XML 文档中使用 XPath 提取文档数据。安装go get -u github.com/antchfx/xqueryHTML 查询这个包使用 Golang 官方包来解析 html 文件:html。方法:Find(html.Node, string) []html.NodeFindOne(*html.Node, string) *html.NodeFindEach(*html.Node, string, func(int, *html.Node))LoadURL(string) *html.Nodepackage main
import (
"golang.org/x/net/html"
"github.com/antchfx/xquery/html"
)
func main() {
html_string:=`<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>`
root, err := html.Parse(strings.NewReader(html_string))
if err != nil {
panic(err)
}
node := htmlquery.FindOne(root, "//title")
fmt.Println(htmlquery.OutputHTML(node)) // output html text with tags
fmt.Println(htmlquery.InnerText(node))
}XML 查询方法:Find(Node, string) []NodeFindOne(*Node, string) *NodeFindEach(*Node, string, func(int, *Node))LoadURL(string) *Node
import (
"golang.org/x/net/html"
"github.com/antchfx/xquery/html"
)
func main() {
html_string:=`<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>`
root, err := html.Parse(strings.NewReader(html_string))
if err != nil {
panic(err)
}
node := htmlquery.FindOne(root, "//title")
fmt.Println(htmlquery.OutputHTML(node)) // output html text with tags
fmt.Println(htmlquery.InnerText(node))
}XML 查询方法:Find(Node, string) []NodeFindOne(*Node, string) *NodeFindEach(*Node, string, func(int, *Node))LoadURL(string) *Node