我正在尝试让 colly 抓取以下页面:https://www56.muenchen.de/termin/index.php?loc=BB .
这是我的代码:
package main
import (
"fmt"
"log"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector(
colly.IgnoreRobotsTxt(),
colly.Async(false),
)
c.OnHTML("html", func(e *colly.HTMLElement) {
fmt.Println(e.Text)
})
c.OnError(func(_ *colly.Response, err error) {
log.Println("Something went wrong:", err)
})
c.Visit("https://www56.muenchen.de/termin/index.php?loc=BB")
c.OnScraped(func(r *colly.Response) {
fmt.Println("Finished")
})
}
问题是网站被访问后加载了一些内容。我不确定如何告诉 colly “等待”直到发生这种情况,然后再查看结果。
期待一些想法。
最佳答案
不能,因为 colly 必须做那个客户端,但是 colly does not execute JavaScript - 所以没有 Ajax。
要模拟浏览器,您可以按照上面的链接建议使用 selenium 或 phantomjs。
关于go - ajax加载网站内容后的网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029385/