随机延时
go
package main
import (
"fmt"
"time"
"github.com/gocolly/colly"
"github.com/gocolly/colly/debug"
)
func main() {
url := "https://httpbin.org/delay/2"
// 实例化 collector
c := colly.NewCollector(
// 关联一个debugger到收集器
colly.Debugger(&debug.LogDebugger{}),
colly.Async(true),
)
// 当访问的链接匹配到 "*httpbin.*" 时, 限制colly启动的线程数为2
c.Limit(&colly.LimitRule{
DomainGlob: "*httpbin.*",
Parallelism: 2,
RandomDelay: 5 * time.Second,
})
// 4个小城开始爬取 https://httpbin.org/delay/2
for i := 0; i < 4; i++ {
c.Visit(fmt.Sprintf("%s?n=%d", url, i))
}
// 开始爬取 https://httpbin.org/delay/2
c.Visit(url)
// 等待线程执行结束
c.Wait()
}