Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

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

请教大佬哪里出错了.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...