开发者社区> 技术小胖子> 正文

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                                                                        


wKiom1MybG7Q9OCWAAgXzjPTBo0978.jpg


具体的用法:

wKiom1MybJyxPy2CAAUE_hjCdcE841.jpg


咱们在看看nginx服务端的日志情况:

wKiom1MybvfyFxAyABD8hLgjayk480.jpg






 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1384473,如需转载请自行联系原作者



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
ASP.NET Core Web API 帮助页
ASP.NET Core Web API 帮助页
116 0
一文概览设计Web API 中的细节
一文概览设计Web API 中的细节
81 0
借助 Web Animations API 实现一个鼠标跟随偏移动画
借助 Web Animations API 实现一个鼠标跟随偏移动画
88 0
WebAPI学习(一)——创建Web API程序
WebAPI学习(一)——创建Web API程序
163 0
几种不常用Web API(振动、重力感应、联网状态、系统电量信息、页面可见性、canvas转base64、监听屏幕旋转、元素全屏显示)
navigator对象中有一些不常用的API,以下主要介绍vibrate振动,deviceorientation重力感应,online联网状态,getBattery系统电量信息,visibilitychange页面可见性,toDataURL(canvas转base64),orientationchange监听屏幕旋转和fullScreen元素全屏显示。
120 0
.NET MVC第十章 vue axios解析web api接口
.NET MVC第十章 vue axios解析web api接口
59 0
.NET MVC第九章、Web Api Json序列化与反序列化
.NET MVC第九章、Web Api Json序列化与反序列化
87 0
.NET MVC第八章、Web Api 跨域接口
.NET MVC第八章、Web Api 跨域接口
77 0
【前端】【H5 API】Web存储 Web Storage
【前端】【H5 API】Web存储 Web Storage
145 0
每日一学—Web API之requestAnimationFrame
If you’ve never written code to animate inside the browser, you can stop reading :) ( ´・・)ノ(._.`)
147 0
+关注
技术小胖子
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
数据资源的基础设施API总线
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多
相关镜像