Golang goquery

goquery地址:

https://github.com/PuerkitoBio/goquery

还需要下载两个goquery需要用的包:

https://github.com/andybalholm/cascadia

https://golang.org\x\net\html

golang.org这个被墙, 可以使用镜像:

https://github.com/golang/net

一、元素、ID选择:

import(

扫描二维码关注公众号,回复: 6987163 查看本文章

   "fmt"

   "log"

   "github.com/PuerkitoBio/goquery"

)

func main(){

   url:="http://www.baidu.com"

   dom,err:=goquery.NewDocument(url)

   if err != nil {

      log.Fatalln(err)

   }

   //使用find()函数查找元素"div"、类".class"或ID"#id", 并使用Each遍历所有匹配结果赋值给selection, 

   dom.Find("div").Each(func(i int, selection *goquery.Selection){

      fmt.Println(selection.Text()) 

   }

}

猜你喜欢

转载自www.cnblogs.com/xsly/p/10990350.html