项目地址:
https://github.com/PuerkitoBio/goquery
用法:
- 从远程获取内容:
package main import ( "fmt" "github.com/PuerkitoBio/goquery" "net/http" ) func main() { url := "https://www.baidu.com" res, _ := http.Get(url) defer res.Body.Close() doc, _ := goquery.NewDocumentFromReader(res.Body) doc.Find(".s_btn_wr").Each(func(i int, s *goquery.Selection) { attr,_ := s.Find("input").Attr("value") fmt.Println(attr) }) } // 输出 百度一下
- 读取本地内容:
package main import ( "fmt" "github.com/PuerkitoBio/goquery" "strings" ) func main() { html := ` <!doctype html> <html lang="en"> <head> <title>Document</title> </head> <body> <div id="one"> <span class="foo">span 元素</span> </div> </body> </html> ` r := strings.NewReader(html) // 创建 reader doc, _ := goquery.NewDocumentFromReader(r) doc.Find("#one").Each(func(i int, s *goquery.Selection) { attr,_ := s.Find("span").Attr("class") fmt.Println(attr) }) } // 输出 foo
- 从远程获取内容:
文档更新时间: 2024-04-20 10:57 作者:lee