使用Go做爬虫首先想到了XPath解析来做于是上网搜在github上找到了一个Go写的Xpath的库貌似是官方维护的
官方地址: http://gopkg.in/xmlpath.v1
xmlpath.v1 中只有三个结构体
Node 代表当前解析出来的节点 并且该节点是被处理过的可以被Path访问的
Path 代表指定的寻找路径
Iter Node的集合
这么讲很不清楚还是上代码讲解吧
package main
import (
"fmt"
"os"
xmlpath "gopkg.in/xmlpath.v1"
)
func main() {
// nodename()
// getFieldValue()
getSpecifiedValue()
}
var file *os.File
var node *xmlpath.Node
func init() {
var err error
file, err = os.OpenFile("t.xml", os.O_RDWR, os.ModePerm)
if err != nil {
panic("openFile failed!!!")
}
//解析文件获得经过处理的并且可以被path访问的Node类型的node
node, err = xmlpath.Parse(file)
if err != nil {
panic("xmlpath parse file failed!!!")
}
}
func nodename() {
//bookstore为根节点编译过后得到一个*Path类型的值
path := xmlpath.MustCompile("bookstore")
//可能会有多本书所以使用path.Iter(node)获取该节点下面的node集合也就是iterator
it := path.I