go語言實現抓取高清圖片_Golang

來源:互聯網
上載者:User

看到有很多python 的,然後寫了個go 的,可以抓取高清圖片

 getp.go

複製代碼 代碼如下:

package main
import (
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "regexp"
    "strconv"
    "strings"
    "sync"
)
var urlist [4]string
var album chan string
var w sync.WaitGroup
var dir string
func main() {
    dir = "tmp/"
    err := os.Mkdir(dir, 0777)
    if err != nil {
        isexist := os.IsExist(err)
        log.Println(isexist)
    }
    album = make(chan string, 200)
    urlist = [4]string{"http://me2-sex.lofter.com/tag/%E7%BE%8E%E5%A5%B3%E6%91%84%E5%BD%B1?page=", "http://me2-sex.lofter.com/tag/%E6%AC%A7%E7%BE%8E?page=", "http://me2-sex.lofter.com/tag/%E6%A8%A1%E7%89%B9?page=", "http://me2-sex.lofter.com/tag/%E7%BE%8E%E5%AA%9B%E9%A6%86?page="}
    for _, v := range urlist {
        for i := 1; i <= 20; i++ {
            url := v + strconv.Itoa(i)
            w.Add(1)
            go GetAlbum(url)
            w.Wait()
        }
    }
}
func GetAlbum(url string) {
    data := GetUrl(url)
    body := string(data)
    part := regexp.MustCompile(`<a class="img" href="(.*)">`)
    match := part.FindAllStringSubmatch(body, -1)
    for _, v := range match {
        album <- v[1]
        w.Add(1)
        go GetItem()
    }
    w.Done()
}
func GetItem() {
    url := <-album
    defer func() {
        ret := recover()
        if ret != nil {
            log.Println(ret)
            w.Done()
        } else {
            w.Done()
        }
    }()
    data := GetUrl(url)
    if len(data) > 10 {
        body := string(data)
        part := regexp.MustCompile(`bigimgsrc="(.*)"`)
        match := part.FindAllStringSubmatch(body, -1)
        for _, v := range match {
            str := strings.Split(v[1], "/")
            length := len(str)
            source := GetUrl(v[1])
            name := str[length-1]
            file, err := os.Create(dir + name)
            if err != nil {
                panic(err)
            }
            size, err := file.Write(source)
            defer file.Close()
            if err != nil {
                panic(err)
            }
            log.Println(size)
        }
    }
}
func GetUrl(url string) []byte {
    ret, err := http.Get(url)
    if err != nil {
        log.Println(url)
        status := map[string]string{}
        status["status"] = "400"
        status["url"] = url
        panic(status)
    }
    body := ret.Body
    data, _ := ioutil.ReadAll(body)
    return data
}

以上所述就是本文的全部內容了,希望對大家熟悉go語言能夠有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.