golang 全能的模拟请求方法(含代理IP功能)

栏目: IT技术 · 发布时间: 4年前

内容简介:我们在做一些自动化业务或者爬虫业务的时候常常要用到模拟请求,例如模拟登录,模拟购买,抓取页面内容等。如果抓取的页面是一个毫无权限校验的普通页面,那只用Get方法即可,但现实往往比较残酷,不是都那么轻易的被你采集。在一个有登录判断的页面,你可能要伪造cookie,header等;如果IP被限制了请求次数,你还需要使用代理IP。一个常规的请求如下图:

前言

我们在做一些自动化业务或者爬虫业务的时候常常要用到模拟请求,例如模拟登录,模拟购买,抓取页面内容等。如果抓取的页面是一个毫无权限校验的普通页面,那只用Get方法即可,但现实往往比较残酷,不是都那么轻易的被你采集。在一个有登录判断的页面,你可能要伪造cookie,header等;如果IP被限制了请求次数,你还需要使用代理IP。

一个常规的请求如下图:

golang 全能的模拟请求方法(含代理IP功能)

代码

方法代码:

package utils  
  
import (  
    "bytes"  
    "encoding/json" 
    "fmt" 
    "io/ioutil" 
    "net/http" 
    "net/url"
    )  

//模拟请求方法
func HttpPost(postUrl string, headers map[string]string, jsonMap map[string]interface{}, proxyIP string) (string, string) {  
   client := &http.Client{}  
   //转换成postBody  
   bytesData, err := json.Marshal(jsonMap)  
   if err != nil {  
      fmt.Println(err.Error())  
      return "", ""  
  }  
   postBody := bytes.NewReader(bytesData)  
  
   //是否使用代理IP  
   if proxyIP != "" {  
      proxy := func(_ *http.Request) (*url.URL, error) {  
         return url.Parse(proxyIP)  
      }  
      transport := &http.Transport{Proxy: proxy}  
      client = &http.Client{Transport: transport}  
   } else {  
      client = &http.Client{}  
   }  
  
   //post请求  
   req, _ := http.NewRequest("POST", postUrl, postBody)  
   for k, v := range headers {  
      req.Header.Add(k, v)  
   }  
   resp, _ := client.Do(req)  
   //返回内容  
   body, _ := ioutil.ReadAll(resp.Body)  
  
   //解析返回的cookie  
   var cookieStr string  
   cookies := resp.Cookies()  
   if cookies != nil {  
      for _, c := range cookies {  
         cookieStr += c.Name + "=" + c.Value + ";"  
        }  
   }  
   return string(body), cookieStr  
}

调用代码:

package main  
  
import (  
    "encoding/json"  
    "fmt" 
    "goShare/utils"
    )  
  
func main() {  
   //模拟登录 获取cookie  
   loginMap := make(map[string]interface{})  
   loginMap["name"] = "yezi" //账号  
   loginMap["password"] = "123456" //密码  
   body, cookiesStr := utils.HttpPost("https://www.xxx.com/login", nil, loginMap, "")  
  
   //body jsonStr转map  
   var jmap map[string]interface{}  
   if err := json.Unmarshal([]byte(body), &jmap); err != nil {  
      fmt.Println("解析失败", err)  
      return  
  }  
  
   //判断登录是否成功  
   if jmap["code"] != "200" {  
      fmt.Println("登录失败", jmap["message"])  
      return  
  }  
  
   //代理IP 可以去网上找免费的或者收费的  
   proxyIP := "47.112.222.179:8000"  
   //组织headers  
   headers := make(map[string]string)  
   headers["cookie"] = cookiesStr  
   //抓取页面  
   body2, _ := utils.HttpPost("https://www.xxx.com/detail/1", headers, nil, proxyIP)  
   if err := json.Unmarshal([]byte(body), &jmap); err != nil {  
      fmt.Println("解析失败", err)  
      return  
  }  
  
   fmt.Println("采集完毕,返回结果:", body2)  
}

总结

本文只是讲了一种模拟请求的场景,关于不同的业务可以灵活变通一下。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Distributed Algorithms: An Intuitive Approach

Distributed Algorithms: An Intuitive Approach

Wan Fokkink / MIT Press / 2018-2-2 / USD 48.00

The new edition of a guide to distributed algorithms that emphasizes examples and exercises rather than the intricacies of mathematical models. This book offers students and researchers a guide to ......一起来看看 《Distributed Algorithms: An Intuitive Approach》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器