Go implementation Grab HD picture _golang

Source: Internet
Author: User

See a lot of Python, and then write a go, you can grab the HD image

Getp.go

Copy Code code as follows:

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
}

The above is the entire content of this article, I hope to be familiar with the go language can help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.