我以前用基本相同的方式做过几个这样的程序(只是不同的域名),但是这次colly没有找到一个链接,只是在访问第一个页面后退出了。有人能看到哪里出了问题吗?*注:为了清楚地了解当前的主题,我省略了程序的某些部分。
*编辑:我已经找到了问题,但没有解决方案。在终端中运行curl https://trendmicro.com/vinfo/us/security/research-and-analysis/threat-reports会返回一个301 permanently错误,但是在浏览器中连接到相同的链接会得到我想要的页面。为什么会发生这种情况?我如何修复它?
*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")
}发布于 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部分,效果非常好
https://stackoverflow.com/questions/54696728
复制相似问题