我试着刮一个简单的网站,看起来像这样:
<html>
<head>
</head>
<body>
<pre>
"Name Surname 1
Name Surname 2
Name Surname 3
Name Surname 4"
</pre>
</body>
</html>编写了一个简单的go代码:
package main
import (
"fmt"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector(
colly.AllowedDomains("thewebsite.com"),
)
c.OnHTML("body", func(e *colly.HTMLElement) {
fmt.Println(e.Text)
})
c.OnResponse(func(r *colly.Response) {
fmt.Println(r.StatusCode)
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.Visit("http://thewebsite.com")
}当我运行这段代码时,我得到以下输出:
Visiting http://thewebsite.com
200所以一切都很好。该网站正在成功开放,但我没有从它得到任何数据。
我试图将c.OnHTML改为pre,body.pre --但它们都没有像我预期的那样起作用。
我在这里错过了什么?
发布于 2022-05-01 17:17:58
我有一个类似的问题,我不得不取消域限制,尽管它似乎是正确的。换句话说,尝试注释掉AllowedDomains()位,如下所示:
c := colly.NewCollector(
//colly.AllowedDomains("thewebsite.com"),
)https://stackoverflow.com/questions/71575017
复制相似问题