1. 项目地址:
    https://github.com/gocolly/colly
  2. 用法:
    package main
    import (
     "fmt"
     "github.com/gocolly/colly/v2"
    )
    func main() {
     url := "https://www.autohome.com.cn/beijing/"
     c := colly.NewCollector()  // colly 外层对象
     c.OnRequest(func(r *colly.Request) {
         r.Headers.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36")  // 模拟浏览器访问
     })
     c.OnHTML("#homepage-focus-live .focus-middle .ui-list li:first-child a:first-child", func(e *colly.HTMLElement) {
         link := e.Attr("href")
         url1 := e.Request.AbsoluteURL(link)  // 获取绝对路径
         c1 := colly.NewCollector()  // colly 内层对象
         c1.OnHTML(".introduce .introduce_content .article-content", func(e1 *colly.HTMLElement) {
             e1.DOM.Find("img").Remove()  // 移除不需要的标签
             fmt.Println(e1.DOM.Html())  // 输出 html
         })
         c1.Visit(url1)
     })
     c.Visit(url)
    }
文档更新时间: 2020-10-23 09:26   作者:lee