go-爬图片
程序员文章站
2023-02-06 16:30:53
go语言爬取图片 注:动态加载出来的爬取不到,或怕取出来图片出错,代码中的网页是可以正常爬取的 go package main import ( "fmt" "io" "net/http" "os" "regexp" "strconv" _ "strings" ) func SaveImg(idx ......
go语言爬取图片
注:动态加载出来的爬取不到,或怕取出来图片出错,代码中的网页是可以正常爬取的
package main import ( "fmt" "io" "net/http" "os" "regexp" "strconv" _ "strings" ) func saveimg(idx int, url string, page chan int) { path := "d:/gogogo/src/go-爬虫/图片爬取/main/images/" + strconv.itoa(idx+1) + ".jpg" f, err := os.create(path) // resp, err1 := http.get(url) if err != nil { fmt.println("err:", err) return } defer f.close() resp, err := http.get(url) if err != nil { fmt.println("err:", err) return } defer resp.body.close() buf := make([]byte, 4096) for { n, err2 := resp.body.read(buf) if n == 0 { break } if err2 != nil && err2 != io.eof { err = err2 return } f.write(buf[:n]) } page <- idx } func httpget(url string) (result string, err error) { resp, err1 := http.get(url) if err1 != nil { err = err1 return } defer resp.body.close() buf := make([]byte, 4096) for { n, err2 := resp.body.read(buf) if n == 0 { break } if err2 != nil && err2 != io.eof { err = err2 return } result += string(buf[:n]) } return } func main() { url := "http://sc.chinaz.com/tupian/ribenmeinv.html" result, err := httpget(url) if err != nil { fmt.println("err:", err) return } // (?s:(.*?)) ret1 := regexp.mustcompile(`<img src2="(?s:(.*?))"`) alls := ret1.findallstringsubmatch(result, -1) page := make(chan int) n := len(alls) for idx, imgurl := range alls { // fmt.println(imgurl[1]) go saveimg(idx, imgurl[1], page) } for i := 0; i < n; i++ { fmt.printf("第%d下载完成\n", <-page) } }
下一篇: 感恩季特惠 仙剑奇侠传iOS版限时打折