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

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

内容简介:我们在做一些自动化业务或者爬虫业务的时候常常要用到模拟请求,例如模拟登录,模拟购买,抓取页面内容等。如果抓取的页面是一个毫无权限校验的普通页面,那只用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)  
}

总结

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


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

查看所有标签

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

Practical Algorithms for Programmers

Practical Algorithms for Programmers

Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99

Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

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

Markdown 在线编辑器