登录
首页 >  Golang >  Go问答

Colly Go包:如何判断错误是否由超时引起?

来源:stackoverflow

时间:2024-02-08 09:15:16 129浏览 收藏

今天golang学习网给大家带来了《Colly Go包:如何判断错误是否由超时引起?》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

一切正常,除了我想重试超时错误,但我不知道如何与生成的特定 client.timeout 错误进行比较。

缺少的部分在此处的注释中:iferrors.is(err, colly.client.timeout)...:

package main

import (
    "crypto/tls"
    "fmt"
    "github.com/gocolly/colly"
    "net/http"
    "os"
    "strings"
)

func main() {
    crawl()
}

func crawl() {
    httpMethod := "https"
    domains := []string{
        "www.myweb1.com",
        "www.myweb2.com",
    }
    for _, domain := range domains {
        // Instantiate default collector
        c := colly.NewCollector(
            colly.Async(true),
            colly.AllowedDomains(domain),
        )
        c.Limit(&colly.LimitRule{Parallelism: 100})
        /* default = 10s = 1000000000 nanoseconds: */
        c.SetRequestTimeout(10 * 1e9)
        c.WithTransport(&http.Transport{
            TLSClientConfig:&tls.Config{InsecureSkipVerify: true},
        })

        // On every a element which has href attribute call callback
        added := false
        c.OnHTML("a[href]", func(e *colly.HTMLElement) {
            url := e.Request.URL.String()
            link := e.Request.AbsoluteURL(e.Attr("href"))
            // Only those links are visited which are in AllowedDomains
            // create a new context to remember the referer (in case of error)
            for _, domain_ok := range domains {
                if (strings.Contains(link, domain_ok)) {
                    ctx := colly.NewContext()
                    ctx.Put("Referrer", url)
                    c.Request(http.MethodGet, link, nil, ctx, nil)
                    // Visit link found on page
                    c.Visit(link)
                    added = true
                    break
                }
            }
            if !added {
                fmt.Fprintf( os.Stdout, "Ignoring %s (%s)\n", link, url)
            }
        })

        // Before making a request print "Visiting ..."
        c.OnRequest(func(r *colly.Request) {
            fmt.Println("Visiting", r.URL.String())
        })
        c.OnError(func(resp *colly.Response, err error) {
            url := resp.Request.URL.String()
            fmt.Fprintf(
                os.Stdout, "ERR on URL: %s (from: %s), error: %s\n", url,
                resp.Request.Ctx.Get("Referrer"), err,
            )
            //if errors.Is(err, colly.Client.Timeout) {
            //    fmt.Fprintf(os.Stdout, "Retry: '%s'\n", url)
            //    r.Retry()
            //}
        })
        urlBase := fmt.Sprintf("%s://%s", httpMethod, domain)
        fmt.Println("Scraping: ", urlBase)
        c.Visit(urlBase)
        c.Wait()
    }
}

正确答案


我想你可以使用这个片段。最有可能的是,由于超出了上下文中的截止日期,因此引发了超时错误。可以尝试一下。

import (
    "context"
    "os"

    "github.com/cockroachdb/errors"
)

func IsTimeoutError(err error) bool {
    if errors.Is(err, context.DeadlineExceeded) {
        return true
    }
    if errors.Is(errors.Cause(err), context.DeadlineExceeded) {
        return true
    }
    if os.IsTimeout(err) {
        return true
    }
    if os.IsTimeout(errors.Cause(err)) {
        return true
    }
    return false
}

以上就是《Colly Go包:如何判断错误是否由超时引起?》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>