首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Colly找不到任何链接

Colly找不到任何链接
EN

Stack Overflow用户
提问于 2019-02-15 02:16:46
回答 1查看 621关注 0票数 0

我以前用基本相同的方式做过几个这样的程序(只是不同的域名),但是这次colly没有找到一个链接,只是在访问第一个页面后退出了。有人能看到哪里出了问题吗?*注:为了清楚地了解当前的主题,我省略了程序的某些部分。

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

*EDIT2:我发现使用curl -L命令会让curl跟随重定向--然后重定向出我需要的网页。但是,我如何将其翻译为colly?因为colly还在处理301错误。

代码语言:javascript
复制
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")
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-15 02:39:31

我已经找到了解决方案。我将我的链接https://trendmicro.com/vinfo/us/security/research-and-analysis/threat-reports插入到https://wheregoes.com/retracer.php中,以查找301重定向到哪里,结果却发现它预先添加了一个www。添加到链接的开头。添加www。添加到初始c.Visit字符串的开头和c.AllowedDomains部分,效果非常好

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

https://stackoverflow.com/questions/54696728

复制
相关文章

相似问题

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