赶知识网

Golang实现http接口调用及web数据抓取[get post模式]

2017-06-30 / 2270次点击 golang
继续扩展我的golang服务端,这边有些数据库是没有权限的,对方给了我webservices的接口,针对异常的数据,我要去抓数据,再次分析,golang貌似没有python那么多的模拟浏览器访问的模块,还好默认的http就支持。 功能一点都不必urllib2 差。。。


   正题!!! 这里是通过golang提供的net/http模块, http.NewRequest来进行数据抓取。 他能实现python下的urllib2的功能 !

原理不多说了,大家直接套用这两个get post的例子吧。

可以任意的加header头,比如怎么加一个浏览器的标识 !

client := &http.Client{]
req, err := http.NewRequest("POST", "http://127.0.0.1", bytes.NewReader(postData))
req.Header.Add("User-Agent", "无敌浏览器")
resp, err := client.Do(req)
defer resp.Body.Close()

下面是完整的例子,可以加更多的Header

#http://xiaorui.cc
package main
     
import (
    "net/http"
    "io/ioutil"
    "fmt"
    "net/url"
)
                 
func main() {
    client := &http.Client{}
    reqest, _ := http.NewRequest("GET", "http://127.0.0.1/", nil)
               
    reqest.Header.Set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    reqest.Header.Set("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3")
    reqest.Header.Set("Accept-Encoding","gzip,deflate,sdch")
    reqest.Header.Set("Accept-Language","zh-CN,zh;q=0.8")
    reqest.Header.Set("Cache-Control","max-age=0")
    reqest.Header.Set("Connection","keep-alive")
    reqest.Header.Set("User-Agent","chrome 100")
                     
    response,_ := client.Do(reqest)
    if response.StatusCode == 200 {
        body, _ := ioutil.ReadAll(response.Body)
        bodystr := string(body);
        fmt.Println(bodystr)
    }
//  reqest, _ = http.NewRequest("POST","http:/127.0.0.1/", bytes.NewBufferString(data.Encode()))
/    respet1,_ := http.NewRequest("POST","http://127.0.0.1/",url.Values{"key":"Value"})
//    reqest1.Header.Set("User-Agent","chrome 100")
//    client.Do(reqest1)
}


我们再来测试下 post获取数据 !

#http://xiaorui.cc
package main
import(
         "fmt"
         "net/http"
         "net/url"
         "io/ioutil"
 )
 
func main(){
        get()
        post()
}
 func get(){
         response,_:=http.Get("http://127.0.0.1/")
         defer response.Body.Close()
         body,_:=ioutil.ReadAll(response.Body)
         fmt.Println(string(body))
               
         if response.StatusCode == 200 {=
                 fmt.Println("ok")
         }else{
                 fmt.Println("error")
         }
 }
 func post(){
         //resp, err :=
         http.PostForm("http://127.0.0.1",
                 url.Values{"name": {"ruifengyun"}, "blog": {"xiaorui.cc"},
                 "aihao":{"python golang"},"content":{"nima,fuck "}})
 }


我们用http加上golang的runtime可以搞成类似 ab的压力测试工具,我昨天写了一个版本,但是在压倒3k以上的链接数的时候,会出现不少的error,原因可能是linux本身没有做tcp的优化,获取是对端的tornado没有用@gen,所以效率跟不上去,我的压力程序没有做defer panic处理。 今天看了下 golang的 gb压力测试工具,发现主要的思路是相同的,但是很多的细节没有做处理,比如channel的同步是用那种for <-c 的土方法实现的。

我的程序是有问题,但是老外有大牛已经构建了一套类似ab的工具,性能差不多,但是这个支持更多的选项和参数,包括代理,基本认证,请求头header信息,长链接,post,gzip压缩,开启几个cpu核心,cookie的插入。

go get github.com/parkghost/gohttpbench
go build -o gb github.com/parkghost/gohttpbench
有用 0 没用 0

Top10

沪ICP备09053415号 © 赶知识网