Golang采集页面简单例子

前端之家收集整理的这篇文章主要介绍了Golang采集页面简单例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import (
  "fmt"
	"regexp"
	"net/http"
	"io/IoUtil"
)
func file_get_contents(url string) string{
    r,e := http.Get(url)
    if e != nil {
        return ""
    }
    defer r.Body.Close()
    c,e := IoUtil.ReadAll(r.Body)
    if e != nil {
        return""
    }
    return string(c)
}
func get_title(url string,c chan string){
	html:=file_get_contents(url)
	r:= regexp.MustCompile(`<title>([^<].+?)<\/title>`)
	rs:=r.FindStringSubmatch(html)
 	c<-rs[1]
}
func main() {
	u:=[]string{}
	u=append(u,"http://www.oschina.net/news/41110/mozilla-firefox-weibo")
	u=append(u,"http://www.oschina.net/news/41080/windows-phone-porting-challenge")
	u=append(u,"http://www.oschina.net/news/41093/music-copyright")
	u=append(u,"http://www.oschina.net/news/41109/rainweather-updated")
	u=append(u,"http://www.oschina.net/news/41091/visual-studio-2013")
	u=append(u,"http://www.oschina.net/news/41090/MysqL-5-6-12-ga")
	u=append(u,"http://www.oschina.net/code/snippet_103482_15911")
	u=append(u,"http://www.oschina.net/code/snippet_212240_21885")
	u=append(u,"http://www.oschina.net/translate/PHP-best-practices")
	u=append(u,"http://www.oschina.net/news/41053/whitehouse-programming-hackathon")
	u=append(u,"http://www.oschina.net/news/41035/android-things-network")
	u=append(u,"http://www.oschina.net/news/41037/github-announces-octokit")
	u=append(u,"http://www.oschina.net/news/41032/linux-opensource-cpu-z")
	u=append(u,"http://www.oschina.net/news/41051/writing-code-is-most-important-in-21-century")
	u=append(u,"http://www.oschina.net/news/41077/15-page-transitions-effects-tutorials-in-css3-and-jquery")
 

	l:=len(u)
	c := make(chan string,2)
	for i:=0;i<l;i++{
		go get_title(u[i],c)
	}
	for i:=0;i<l;i++{
		fmt.Println(<-c);
	}
}
原文链接:https://www.f2er.com/go/191282.html

猜你在找的Go相关文章