加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang采集页面简单例子

发布时间:2020-12-16 18:59:26 所属栏目:大数据 来源:网络整理
导读:package mainimport ( "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)}
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);
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读