colly 擴展 ##10

擴展是Colly附帶的小型輔助實用程序。插件列表能夠在這裏找到git

 

實例

下面的示例啓用了隨機用戶代理切換器和引用設置器擴展,並兩次訪問httpbin.org。github

import (
    "log"

    "github.com/gocolly/colly"
    "github.com/gocolly/colly/extensions"
)

func main() {
    c := colly.NewCollector()
    visited := false

    extensions.RandomUserAgent(c)
    extensions.Referrer(c)

    c.OnResponse(func(r *colly.Response) {
        log.Println(string(r.Body))
        if !visited {
            visited = true
            r.Request.Visit("/get?q=2")
        }
    })

    c.Visit("http://httpbin.org/get")
}
相關文章
相關標籤/搜索