一个很好的例子是用户代理切换器,它在每次请求时都会改变用户代理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" func RandomString() string { b := make([]byte, rand.Intn(10)+10) for i := range b { b[i] = letterBytes[rand.Intn(len(letterBytes))] } return string(b) } c := colly.NewCollector() c.OnRequest( func (r *colly.Request) { r.Headers.Set( "User-Agent" , RandomString()) }) |