goJquery 像 jQuery一样操作DOM
最后更新于:2022-04-02 02:37:09
[TOC]
> [github](https://github.com/PuerkitoBio/goquery)
## 安装
`go get github.com/PuerkitoBio/goquery`
## 实例
### helloworld
```
package main
import (
"fmt"
"log"
"github.com/PuerkitoBio/goquery"
)
func ExampleScrape() {
doc, err := goquery.NewDocument("http://metalsucks.net")
if err != nil {
log.Fatal(err)
}
// Find the review items
doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
band := s.Find("a").Text()
title := s.Find("i").Text()
fmt.Printf("Review %d: %s - %s\n", i, band, title)
})
}
func main() {
ExampleScrape()
}
```
## 获取`href`等属性
```
url,bool := s.Find(".file-right a").Attr("href")
```
';