安装colly
go get -u github.com/gocolly/colly
第一个colly 应用
package main
import (
"fmt"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector()
// goquery selector class
c.OnHTML(".sidebar_link", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
/* link := e.Attr("href")
// Print link
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
// Visit link found on page
// Only those links are visited which are in AllowedDomains
c.Visit(e.Request.AbsoluteURL(link))*/
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("url:",r.URL)
})
c.Visit("https://gorm.io/zh_CN/docs/")
}
回调函数的调用顺序