Golang Collecting Pictures

Source: Internet
Author: User
Tags sprintf
This is a creation in Article, where the information may have evolved or changed.

help friends to collect, see he opened a South Korean web site a point to save see not go down the speed is too slow, originally wanted to write with PHP, just see no smell big go collection, and then shameless changed the next use ,

Package Main


Import (
"FMT"
"IO"
"Io/ioutil"
"Log"
"Net/http"
"OS"
"Path"
"RegExp"
"Strings"
)

Type Notfounderror struct {
Message string
}

Func (e notfounderror) Error () string {
Return E.message
}

Type Remoteerror struct {
Host string
ERR Error
}

Func (e *remoteerror) Error () string {
Return E.err.error ()
}

var useragent = "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/29.0.1541.0 safari/537.36 "

HttpGet gets the specified resource. Errnotfound is returned if the
Server responds with status 404.
Func httpget (client *http. Client, URL string, header http. Header) (IO. Readcloser, error) {
Req, Err: = http. Newrequest ("GET", url, nil)
If err! = Nil {
return nil, err
}
Req. Header.set ("User-agent", useragent)
For k, vs: = Range Header {
Req. Header[k] = vs
}
RESP, err: = client. Do (req)
If err! = Nil {
return nil, &remoteerror{req. Url. Host, err}
}
If resp. StatusCode = = 200 {
Return resp. Body, Nil
}
Resp. Body.close ()
If resp.  StatusCode = = 404 {//403 can be the rate limit error. || Resp. StatusCode = = 403 {
Err = notfounderror{"Resource not Found:" + URL}
} else {
Err = &remoteerror{req. Url. Host, FMT. Errorf ("Get%s-%d", url, resp.) StatusCode)}
}
return nil, err
}

Httpgetbytes gets the specified resource. Errnotfound is returned if the server
responds with status 404.
Func httpgetbytes (client *http. Client, URL string, header http. Header) ([]byte, error) {
RC, Err: = HttpGet (client, URL, header)
If err! = Nil {
return nil, err
}
Defer RC. Close ()
Return Ioutil. ReadAll (RC)
}

Httpgettofile gets the specified resource and writes to file.
Errnotfound is returned if the server responds with status 404.
Func httpgettofile (client *http. Client, URL string, header http. Header, fileName string) error {
RC, Err: = HttpGet (client, URL, header)
If err! = Nil {
return err
}
Defer RC. Close ()


Os. Mkdirall (path. Dir (fileName), OS. Modeperm)
F, err: = OS. Create (FileName)
If err! = Nil {
return err
}
Defer F.close ()
_, err = Io. Copy (f, RC)
return err
}


var img = regexp. Mustcompile (' href=\ ' javascript:goview\ (\d+)
var imgpattern = RegExp. Mustcompile (' id= ' Mainimage "src=\"). /upload (. *?). JPG ')
var totaltask int

Func Download (URL string, num chan bool) {
url = strings. Trimprefix (URL, ' href= ' Javascript:goview (')
Page: = "http://www.gdweb.co.kr/main/koreaWebView.asp?idx=%s&url=koreaWeb.asp"
T, err: = Httpgetbytes (&http. client{}, FMT. Sprintf (page, url), nil)


If err! = Nil {
Log. Fatalf ("Fetch page failed:%v", err)
}
Matches: = Imgpattern.findall (T,-1)
For _, Match: = range matches {
url = "http://www.gdweb.co.kr" + strings. Trimprefix (String (match), ' id= ' Mainimage "src=": `)
Log. Printf ("Downloading:%s", URL)
ERR: = Httpgettofile (&http. client{}, URL, nil, "pics/" +path. Base (URL))
If err! = Nil {
Log. Printf ("Picture download failed (%s):%v", url, err)
}
}
totaltask--
<-num
}

Func Main () {
Control simultaneous Download Quantities
Num: = Make (chan bool, 5)


Main thread Crawl page, child threads download picture
BASEURL: = "http://nvmingxing.net/hotness/%d/"
Abaseurl: = "http://www.gdweb.co.kr/main/koreaWebView.asp?idx=8200&url=koreaWeb.asp"
BASEURL: = "http://www.gdweb.co.kr/main/koreaWeb.asp?idx=&url=index.asp&lpage=124&page=%d"
For I: = 2; I < 124; i++ {
Log. Printf ("Crawl page:%d", totaltask)
Data, err: = Httpgetbytes (&http. client{}, FMT. Sprintf (BASEURL, i+1), nil)
If err! = Nil {
Log. Fatalf ("Fetch page failed (%d):%v", I, err)
}
Matches: = img. FINDALL (data,-1)
For _, Match: = range matches {
totaltask++
Num <-True
Go Download (string (match), num)
}
}
}
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.