golang 爬虫 colly 发送帖子请求
最编程
2024-04-08 11:22:08
...
继续还是工作中使用colly,不管是官网,还是网上的一些文章(其实90%就是把官网的案例抄过去),都是一样的格式,没有讲到post,测试了几次,记录一下post的使用
c := colly.NewCollector() type data struct { Phone string `json:"phone" binding:"required"` } d:=&data{ Phone:"18190897361", } da,err:=json.Marshal(d) if err!=nil{ fmt.Println(err) } c.OnResponse(func(response *colly.Response) { fmt.Println(string(response.Body)) }) c.OnRequest(func(r *colly.Request) { fmt.Println(r) fmt.Println(r.Method) r.Headers.Set("Content-Type", "application/json;charset=UTF-8") r.Headers.Set("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36") }) c.OnError(func(response *colly.Response, e error) { fmt.Println(e) }) c.PostRaw("http://www.××××.com:×××/baseDevice/getUserInfo",da) //c.Visit("http://www.××××.com:×××/baseDevice/getUserInfo")
其实也没多少不一样,首先准备你要发送的json格式的数据(现在90%都是json格式请求了),
type data struct { Phone string `json:"phone" binding:"required"` } d:=&data{ Phone:"18190897361", } da,err:=json.Marshal(d)
这里只发送一个电话号码,第二部就是最后的发送了
c.PostRaw("http://www.××××.com:×××/baseDevice/getUserInfo",da)
1:这句话一定要写到最后
c.Visit(“http://www.××××.com:×××/baseDevice/getUserInfo”)
Visit方法,点进去源码可以看到默认走的是get模式,我们这里发送post,就不需要写了
就这么简单—结束
到此这篇关于golang爬虫colly 发送post请求的文章就介绍到这了,更多相关golang colly 发送post请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 在 Docker 中安装 Centos7 操作系统
下一篇: golang 多核编译
推荐阅读
-
Golang Colly 下载小红书详情页图像的小型爬虫
-
golang 爬虫 colly 发送帖子请求
-
Python爬虫实现电影资源下载方法-本人python运行环境: IDE丨pycharm2.3 版本丨Python3.6 系统丨Windows 10 爬虫目的与思路:实现目的与思路: 目的: 实现对电影目标url的解析与下载,由于第三方vip解析,只提供在线观看,隐藏想实现对目标视频的下载。 思路: 1.在百度搜索全名解析得到解析网站:jx.618g.com?/url=[电影播放地址]。 2.首先拿到想要看的电影url,通过第三方vip视频解析网站进行解析。 3.通过抓包,模拟浏览器发送正常请求。 4.通过拿到缓存ts文件,下载视频ts文件。 5.最后通过转换为mp4文件,即可实现正常播放。主要代码: