go语言实现抓取高清图片
程序员文章站
2022-11-15 12:51:59
看到有很多python 的,然后写了个go 的,可以抓取高清图片
getp.go
复制代码 代码如下:
package main
import (
&...
看到有很多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语言能够有所帮助。
上一篇: PHP使用JSON和将json还原成数组
下一篇: Go语言实现顺序存储的线性表实例