colly:invalid memory address or nil pointer dereference

今天用colly写了个demo,下面是代码

func downloadImages(e *colly.HTMLElement) {
    srcRef := destUrl + e.Attr("src")
    res, _ := http.Get(srcRef)
    if strings.Contains(srcRef, "http") && strings.Contains(srcRef, ".jpg") {
        arr := strings.Split(srcRef, "/")
        fileName := strings.Split(arr[len(arr)-1], ".")[0]
        fmt.Println("fileName", fileName)
        f, _ := os.Create("./images/" + fileName + ".jpg")
        defer f.Close()
        written, _ := io.Copy(f, res.Body)

        fmt.Println("info", written)
    }
}

下面是出错信息:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x48 pc=0x868eeb]

goroutine 1 [running]:
main.downloadImages(0xc000256240)
 C:/Users/DELL/Desktop/work/golang/colly/colly.go:28 +0x32b
main.main.func1(0xc000256240)
 C:/Users/DELL/Desktop/work/golang/colly/colly.go:39 +0x32
github.com/gocolly/colly.(\*Collector).handleOnHTML.func1(0x6, 0xc000248390)
 C:/Users/DELL/go/pkg/mod/github.com/gocolly/colly@v1.2.0/colly.go:963 +0x93
github.com/PuerkitoBio/goquery.(\*Selection).Each(0xc00020bb00, 0xc000099cb0, 0x6)
 C:/Users/DELL/go/pkg/mod/github.com/!puerkito!bio/goquery@v1.6.0/iteration.go:10 +0x5a
github.com/gocolly/colly.(\*Collector).handleOnHTML(0xc00006b860, 0xc0001620c0, 0x0, 0x0)
 C:/Users/DELL/go/pkg/mod/github.com/gocolly/colly@v1.2.0/colly.go:953 +0x222
github.com/gocolly/colly.(\*Collector).fetch(0xc00006b860, 0xc00001ed40, 0x14, 0x9792e0, 0x3, 0x1, 0x0, 0x0, 0xc000055fb0, 0xc00012cb70, ...)
 C:/Users/DELL/go/pkg/mod/github.com/gocolly/colly@v1.2.0/colly.go:623 +0x44d
github.com/gocolly/colly.(\*Collector).scrape(0xc00006b860, 0xc00001ed40, 0x14, 0x9792e0, 0x3, 0x1, 0x0, 0x0, 0x0, 0xc00012cb70, ...)
 C:/Users/DELL/go/pkg/mod/github.com/gocolly/colly@v1.2.0/colly.go:535 +0x46b
github.com/gocolly/colly.(\*Collector).Visit(0xc00006b860, 0x98544c, 0x14, 0x9a3198, 0x8b2a00)
 C:/Users/DELL/go/pkg/mod/github.com/gocolly/colly@v1.2.0/colly.go:412 +0x89
main.main()
 C:/Users/DELL/Desktop/work/golang/colly/colly.go:55 +0xd7
exit status 2

请教大佬哪里出错了.

阅读 2.1k
1 个回答

colly.go 第55行出现了空指针引用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏