Go 语言采集页面简单例子

Go 语言采集页面简单例子,代码如下:

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);
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。