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
请教大佬哪里出错了.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
colly.go
第55行出现了空指针引用