转载请标明出处:
http://blog.csdn.net/qq_27818541/article/details/111996916
本文出自:【BigManing的博客】
示例介绍
- 首先访问入口
主URL
- 在
c.OnError
方法中定义 处理异常的逻辑
示例代码
package main
import (
"fmt"
"github.com/gocolly/colly/v2"
)
func main() {
// Create a collector
c := colly.NewCollector()
// Set HTML callback
// Won't be called if error occurs
c.OnHTML("*", func(e *colly.HTMLElement) {
fmt.Println(e)
})
// Set error handler
c.OnError(func(r *colly.Response, err error) {
fmt.Println("Request URL:", r.Request.URL, "failed with response:", r, "\nError:", err)
})
// Start scraping
c.Visit("https://definitely-not-a.website/")
}
输出
Request URL: https://definitely-not-a.website/ failed with response: &{0 [] 0xc0001fbf30 0xc000124380 <nil> <nil>}
Error: Get "https://definitely-not-a.website/": dial tcp: lookup definitely-not-a.website: no such host