首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GoColly为每个请求设置代理

GoColly为每个请求设置代理
EN

Stack Overflow用户
提问于 2019-09-02 17:26:21
回答 1查看 871关注 0票数 1

我开始和科利打交道。我正在尝试为每个请求设置一个代理。但并不是所有的请求都通过代理

它应该可以工作,https://github.com/gocolly/colly/blob/master/request.go#L52

我已经将此代码添加到我的代码中。但是请求像以前一样没有代理。

代码语言:javascript
复制
c.OnRequest(func(r *colly.Request) { 
  r.ProxyURL = "..." 
}

也许我没有正确理解这应该是如何工作的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-02 17:38:42

您是否尝试过在收集器上设置ProxyURL,而不是在回调函数中设置它?

代码语言:javascript
复制
// SetProxy sets a proxy for the collector. This method overrides the previously
// used http.Transport if the type of the transport is not http.RoundTripper.
// The proxy type is determined by the URL scheme. "http"
// and "socks5" are supported. If the scheme is empty,
// "http" is assumed.
func (c *Collector) SetProxy(proxyURL string) error {
    proxyParsed, err := url.Parse(proxyURL)
    if err != nil {
        return err
    }

    c.SetProxyFunc(http.ProxyURL(proxyParsed))

    return nil
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57754483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档