登录
首页 >  Golang >  Go问答

没有找到与Colly相关的链接

来源:stackoverflow

时间:2024-03-07 20:54:22 457浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《没有找到与Colly相关的链接》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我之前已经以基本相同的方式(只是不同的域)完成了一些类似的程序,但是这一次,colly 没有找到单个链接,并且在访问第一页后就退出了。谁能看出出了什么问题吗? *注意:为了清楚地说明当前的主题,我省略了程序的某些部分。

*编辑:我发现了问题,但没有解决方案。运行 curl https://trendmicro.com/vinfo/us/security/research-and-analysis/threat-reports 在终端中返回 301 永久移动错误,但连接到浏览器中的同一链接会得到我想要的页面。为什么会发生这种情况以及如何解决它?

*edit2:我发现执行命令 curl -l 会使curl遵循重定向 - 然后输出我需要的网页。但是,我该如何将其翻译为 colly 呢?因为 colly 仍然发现 301 错误。

import (
    "fmt"
    "strings"
    "github.com/gocolly/colly"
)

func main() {
    /* only navigate to links within these paths */
    tld1 := "/vinfo/us/security/research-and-analysis/threat-reports"

    c := colly.NewCollector(
        colly.AllowedDomains("trendmicro.com", "documents.trendmicro.com"),
    )

    c.OnHTML("a[href]", func(e *colly.HTMLElement) {
        link := e.Attr("href")
        fmt.Printf("Link found: %q -> %s\n", e.Text, link)
        if strings.Contains(link, tld1) {
            c.Visit(e.Request.AbsoluteURL(link))
        }
    })

    c.OnRequest(func(r * colly.Request) {
        fmt.Println("Visiting", r.URL.String())
    })

    c.Visit("https://trendmicro.com/vinfo/us/security/research-and-analysis/threat-reports")
}

解决方案


我已经找到解决办法了。我将链接 https://trendmicro.com/vinfo/us/security/research-and-analysis/threat-reports 插入 https://wheregoes.com/retracer.php 以查找 301 重定向到的位置,却发现它前面有一个 www。到链接的开头。添加 www.到初始 c.Visit 字符串的开头和 c.AllowedDomains 部分就像一个魅力

好了,本文到此结束,带大家了解了《没有找到与Colly相关的链接》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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