You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Golang Colly爬取Google Trends遇Too Many Requests错误求助

解决Colly爬取Google Trends时的Too Many Requests错误

你遇到的错误信息翻译为:

出错了。
抱歉,您最近向我们发送的请求过多。请稍后再试。我们仅了解这些信息。

Google Trends的反爬机制比普通网站严格,会检测请求频率、请求头特征,单IP短时间内重复请求很容易触发限流,这就是只爬它报错、其他网站正常的原因。下面是具体解决办法:

1. 模拟浏览器请求头

Google会识别非浏览器发起的请求,给请求添加标准的浏览器头信息:

c.OnRequest(func(r *colly.Request) {
    fmt.Println("Scraping:", r.URL)
    // 可替换为当前主流浏览器的User-Agent
    r.Headers.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
    r.Headers.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
    r.Headers.Set("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3")
    r.Headers.Set("Referer", "https://www.google.com/")
})

2. 限制请求频率

通过Colly的Limit规则控制请求间隔,避免短时间内发起大量请求:

err := c.Limit(&colly.LimitRule{
    DomainGlob:  "*trends.google.com*",
    Delay:       3 * time.Second,  // 基础延迟
    RandomDelay: 1 * time.Second,  // 随机增加延迟,避免固定间隔被识别
})
if err != nil {
    log.Fatal(err)
}

3. 启用缓存与Cookie管理

启用缓存可以避免重复请求相同URL,同时保持Cookie会话,让请求更接近真实用户行为:

c := colly.NewCollector(
    colly.CacheDir("./trends_cache"),  // 缓存目录
    colly.AllowURLRevisit(),
)

4. 使用代理IP轮换(高请求量场景)

如果需要批量爬取,单IP容易被封,可以维护一个代理列表,每次请求随机切换不同IP:

// 示例:设置单个代理,实际可扩展为代理池随机选择
c.SetProxy("http://your-proxy-ip:port")

修改后的完整代码

import (
    "fmt"
    "log"
    "strings"
    "time"

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

func Teste(searchTrend string) {
    searchTrend = strings.Trim(searchTrend, " ")
    searchTrend = strings.Replace(searchTrend, " ", "%20", -1)

    linkTrends := "https://trends.google.com.br/trends/explore?geo=BR&q=" + searchTrend

    // 初始化Collector,启用缓存
    c := colly.NewCollector(
        colly.CacheDir("./trends_cache"),
        colly.AllowURLRevisit(),
    )

    c.SetRequestTimeout(120 * time.Second)

    // 设置请求频率限制
    if err := c.Limit(&colly.LimitRule{
        DomainGlob:  "*trends.google.com*",
        Delay:       3 * time.Second,
        RandomDelay: 1 * time.Second,
    }); err != nil {
        log.Fatal(err)
    }

    // 模拟浏览器请求头
    c.OnRequest(func(r *colly.Request) {
        fmt.Println("Scraping:", r.URL)
        r.Headers.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
        r.Headers.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        r.Headers.Set("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3")
        r.Headers.Set("Referer", "https://www.google.com/")
    })

    c.OnResponse(func(r *colly.Response) {
        fmt.Println("Status:", r.StatusCode)
    })

    c.OnError(func(r *colly.Response, e error) {
        log.Println("error:", e, r.Request.URL, string(r.Body))
    })

    c.OnHTML("div.widget-template", func(h *colly.HTMLElement) {
        // 你的数据解析逻辑
    })

    c.Visit(linkTrends)
}

内容的提问来源于stack exchange,提问作者Maick Machado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:55:21