利用 channel (管道) 技术多线程下载图片.
package main import ( "io/ioutil" "log" "net/http" "os" "regexp" //"strconv" "strings" "sync" ) var urlist = [...]string{"http://stock.591hx.com/article/2014-12-03/0000850005s.shtml"} var album chan string var w sync.WaitGroup var dir string func main() { dir = "tmp_chenjo/" err := os.Mkdir(dir, 0777) if err != nil { isexist := os.IsExist(err) log.Println(isexist) } album = make(chan string, 200) for _, v := range urlist { w.Add(1) go GetAlbum(v) w.Wait() } } func GetAlbum(url string) { data := GetUrl(url) body := string(data) //<img src="http://stock.591hx.com/images/hnimg/201412/03/64/13418266510200941552.jpg" alt="" /></p> part := regexp.MustCompile(`<img src="(.*)" alt="" />`) match := part.FindAllStringSubmatch(body, -1) for _, v := range match { if m, _ := regexp.MatchString(`.*/hnimg/201412/03/.*\.jpg`, v[1]); !m { continue } //println(v[1]) album <- v[1] w.Add(1) go GetItem() } w.Done() } func GetItem() { url := <-album println(url) 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(url, "/") length := len(str) source := GetUrl(url) 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 }
console output:
G:/share/Golang/Golang.exe [G:/share/Golang]
2015/01/18 21:54:29 true
http://stock.591hx.com/images/hnimg/201412/03/64/13418266510200941552.jpg
http://stock.591hx.com/images/hnimg/201412/03/7/10886141709285175583.jpg
http://stock.591hx.com/images/hnimg/201412/03/0/5391574706574741364.jpg
http://stock.591hx.com/images/hnimg/201412/03/1/5405780767459854941.jpg
http://stock.591hx.com/images/hnimg/201412/03/16/13722698761317688276.jpg
http://stock.591hx.com/images/hnimg/201412/03/7/16853951343108680551.jpg
http://stock.591hx.com/images/hnimg/201412/03/50/17680852843413447062.jpg
http://stock.591hx.com/images/hnimg/201412/03/34/14366548421421579970.jpg
http://stock.591hx.com/images/hnimg/201412/03/4/17141924098089490820.jpg
2015/01/18 21:54:30 29968
2015/01/18 21:54:30 28697
2015/01/18 21:54:30 51586
2015/01/18 21:54:30 53031
2015/01/18 21:54:30 31234
2015/01/18 21:54:31 47618
2015/01/18 21:54:31 54649
2015/01/18 21:54:31 40422
2015/01/18 21:54:31 155527
成功: 进程退出代码 0.
相关推荐
基于golang实现的多线程下载器,用于下载支持http/https协议的资源,具体源码可移步个人博客(地址:http://t.csdn.cn/0aR0i)。
Golang:通过Gin框架+Redis+责任链,实现一个简单的钉钉机器人,进行消息处理 ps:多应用版
此程序将数据存入mysql
golang:golang练习
高朗归纳了Golang语言的完整知识体系,包括Golang基本概念,经典代码案列以及其他相关资料等Go工具的命令文档go程序管理Go源代码以及运行这里列出的其他命令Cgo能够创建调用C代码的Go包Cover是一个有用的创建和分析...
golang:golang学习记录
Golang:如何优雅的让所有子协程执行完后再执行主协程 方法一:channel 实现同步 使用 channel 来完成同步功能。 /* @Time : 2019-10-28 下午 5:30 @Author : Gerald @File : 用channel来实现同步 @Software: GoLand...
$ docker run --name golang bitnami/golang:latest Docker撰写 $ curl -sSL https://raw.githubusercontent.com/bitnami/bitnami-docker-golang/master/docker-compose.yml > docker-compose.yml $ docker-compose ...
GoRef: golang简单的,线程安全的调用跟踪器。为生产使用而设计的简单golang分析器
learningign-golang:learnign-golang
pre-commit-golang:用于预提交的Golang钩子
golang 1.4.1 win64压缩包。zip格式太大,用7zip重压了一次
blockchain_golang:简单的区块链架构
IKTA-Golang:使用Golang的一些练习
golang:Lerning Go回购
golang爬虫源码,支持图片列表以及单页面图片爬取,对于初学者有参考意义。该版本支持采集列表规则,页码{{n}}为变量 如:《https://www.58pic.com/collect/fav-{{n}}.html》 其中n为页码,起始页码一般默认为1 ####...
提交前的golang 用于golang钩子 使用这些钩子 将此添加到您的.pre-commit-config.yaml - repo: git://github.com/joker8023/pre-commit-golang rev: master hooks: - id: go-fmt - id: go-vet - id: go-lint ...
vagrant-golang:另一个使用go语言的无业游民项目
estudos-golang:自然语言