Golang实现web api接口调用及web数据抓取[get post模式]
前沿:
? ?繼續擴展我的golang服務端,這邊有些數據庫是沒有權限的,對方給了我webservices的接口,針對異常的數據,我要去抓數據,再次分析,golang貌似沒有python那么多的模擬瀏覽器訪問的模塊,還好默認的http就支持。 功能一點都不必urllib2 差。。。
? ?正題!!! 這里是通過golang提供的net/http模塊, http.NewRequest來進行數據抓取。 他能實現python下的urllib2的功能 !
原文:http://rfyiamcool.blog.51cto.com/1030776/1384473 ?
原理不多說了,大家直接套用這兩個get post的例子吧。
可以任意的加header頭,比如怎么加一個瀏覽器的標識 !
| 1 2 3 4 5 | 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
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #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獲取數據 !
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #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的插入。
| 1 2 | go?get?github.com/parkghost/gohttpbench go build -o gb github.com/parkghost/gohttpbench |
用golang實現的搞并發的壓力測試工具 !
原文:xiaoruicc
?-A="": Add Basic WWW Authentication, the attributes are a colon separated username and password. ? ? ? ?
?-C=[]: Add cookie, eg. 'Apache=1234. (repeatable) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-G=4: Number of CPU ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-H=[]: Add Arbitrary header line, eg. 'Accept-Encoding: gzip' Inserted after all normal header lines. (r
epeatable) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-T="text/plain": Content-type header for POSTing, eg. 'application/x-www-form-urlencoded' Default is 'te
xt/plain' ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-c=1: Number of multiple requests to make ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-h=false: Display usage information (this message) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-i=false: Use HEAD instead of GET ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-k=false: Use HTTP KeepAlive feature ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-n=1: Number of requests to perform ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-p="": File containing data to POST. Remember also to set -T ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-r=false: Don't exit when errors ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-t=0: Seconds to max. wait for responses ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-u="": File containing data to PUT. Remember also to set -T ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-v=0: How much troubleshooting info to print ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?-z=false: Use HTTP Gzip feature ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
具體的用法:
咱們在看看nginx服務端的日志情況:
本文轉自 rfyiamcool 51CTO博客,原文鏈接:http://blog.51cto.com/rfyiamcool/1384473,如需轉載請自行聯系原作者
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Golang实现web api接口调用及web数据抓取[get post模式]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: realme GT3 240W 手机现身
- 下一篇: 揭开AS程序的外纱(四) -- 全屏模式