【Go】并发抓取妹子图片

环境准备

  • go get github.com/PuerkitoBio/goquery
  • go get github.com/satori/go.uuid

代码

package main

import (
    "bytes"
    "fmt"
    "log"
    "strconv"
    "github.com/PuerkitoBio/goquery" // 解析html
    "io"
    "io/ioutil"
    "net/http"
    "os"
    "github.com/satori/go.uuid"    // 生成图片文件名
) 

func getAllUrls() []string {
    // http://www.meizitu.com/a/more_1.html
    // http://www.meizitu.com/a/more_72.html
    var urls []string
    var url string
    for i := 0; i < 72; i++ {
        url = "http://www.meizitu.com/a/more_" + strconv.Itoa(i+1) + ".html"
        urls = append(urls, url)
    }
    return urls
}


func parseHtml(url string) int {
    doc, err := goquery.NewDocument(url)
    if err != nil {
        log.Fatal(err)
    }
    doc.Find(".pic > a > img").Each(func(i int, s *goquery.Selection) {

        img_url, _ := s.Attr("src")
        // 启动协程下载图片
        go download(img_url)
    })

    return 0
}

// 下载图片
func download(img_url string) int {
    uid, _ := uuid.NewV4()
    file_name := uid.String() + ".jpg"
    fmt.Println(file_name)

    resp, _ := http.Get(img_url)
    body, _ := ioutil.ReadAll(resp.Body)
    out, _ := os.Create(file_name)
    io.Copy(out, bytes.NewReader(body))

    return 0
}

func main()  {
    urls := getAllUrls()
    for _, url := range urls {
        parseHtml(url)
    }
}

效果

  • 1297张图片,1分钟全部下载完成

【Go】并发抓取妹子图片_第1张图片

你可能感兴趣的:(Web)