go版下載妹子圖

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport(   "fmt"   "io/ioutil"   "net/http"   "regexp"   "strings"   "os"   "image/png"   "image/jpeg"   "image")var filePath = "F:/girls/"func fileExist(fileName string) bool {   if _,ok:=os.Stat(fileName);ok == nil{      return true   }   return false}func Substr(str string, start, length int) string {    rs := []rune(str)    rl := len(rs)    end := 0    if start < 0 {        start = rl - 1 + start    }    end = start + length    if start > end {        start, end = end, start    }    if start < 0 {        start = 0    }    if start > rl {        start = rl    }    if end < 0 {        end = 0    }    if end > rl {        end = rl    }    return string(rs[start:end])}func getImageList(url string, c chan int){   fmt.Println("get page link url==>", url)   body:=getUrl(url)   if body == ""{      return   }   reg := regexp.MustCompile("http://www.meizitu.com/a/[0-9]+.html")   links:=reg.FindAllString(body, -1)   getImageLink(links, c)}func getImageLink(links []string, c chan int){   for _, uri := range links{      fmt.Println("Get images url, page link==>", uri)         body:=getUrl(uri)         if ""==body{            return         }         reg:=regexp.MustCompile("http://pic.meizitu.com/wp-content/uploads/[^\\.]+\\.(jpg|png|gif)")         images:=reg.FindAllString(body, -1)         downloadImage(images)   }   c <- 1}func downloadImage(images []string){   for _,v:=range images{      fmt.Println("Download image, url==>", v)      imageType:=Substr(v, -2, 3)      resp,ok:=http.Get(v)      if nil!=ok{         continue      }      defer resp.Body.Close()      flag:=false      var iImage image.Image      content,ok:=ioutil.ReadAll(resp.Body)      body:=string(content)      if imageType=="jpg"{         iImage,ok=jpeg.Decode(strings.NewReader(body))         flag=true         if nil!=ok{            continue         }      } else if imageType == "png"{         iImage,ok=png.Decode(strings.NewReader(body))         flag=true         if nil!=ok{            continue         }      }      if flag{         rect:=iImage.Bounds()         if rect.Max.X < 200 || rect.Max.Y < 200{            //只下載大圖,小圖跳過            fmt.Println("Skip download image, url ==>", v)            continue         }      }      // body:=getUrl(v)      if nil!=ok || "" == body{         fmt.Println("content is null")         continue      }      paths:=strings.Split(v,"/")      len:=len(paths)      fileName:=filePath + paths[len-4]+  paths[len-3]+  paths[len-2] +  paths[len-1]      if fileExist(fileName){         continue      }      f,ok:=os.Create(fileName)      if ok!=nil{         fmt.Println("open file error")         return      }      defer f.Close()      f.WriteString(body)   }}func getUrl(url string) string{   resp,ok:=http.Get(url)   if nil!=ok{      return ""   }   defer resp.Body.Close()   str,ok:=ioutil.ReadAll(resp.Body)   if ok!=nil{      return ""   }   return string(str)}func main() {   fms:="http://www.meizitu.com/a/sifang_5_%d.html"   max_page:=10   cur_page:=1   offset:=cur_page+max_page   ch:=make(chan int, max_page)   for ;cur_page<offset;cur_page++{      go func(page int){         url:=fmt.Sprintf(fms, page)         fmt.Println("Parse url:",url)         getImageList(url, ch)      }(cur_page)   }   sum:=0   forEnd:   for{      select{      case <- ch:         sum+=1         if sum == max_page{            break forEnd         }      }   }   fmt.Println("done!")}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.