zoukankan      html  css  js  c++  java
  • go 通过代理服务器获取网站内容

    代码:

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "net/http"
        "net/url"
    )
    
    func main() {
        // 这里用简化的UA 访问百度自己
        const (
            uri       = "https://2020.ip138.com/"
            userAgent = "Mozilla/5.0 Baiduspider/2.0"
            dailiuri
        )
        //设置代理服务器,从网上找的
        proxy := func(_ *http.Request) (*url.URL, error) {
            return url.Parse("http://117.88.5.118:3000")
        }
        transport := &http.Transport{Proxy: proxy}
        // 创建client
        client := &http.Client{Transport: transport}
        // 创建请求
        req, _ := http.NewRequest("GET", uri, nil)
    
        // 在请求头中添加指定的UA
        req.Header.Add("User-Agent", userAgent)
        // 发起请求并返回结果
        resp, err := client.Do(req)
    
        if err != nil {
            fmt.Println(err)
            return
        }
        // 读取资源数据
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println(string(body))
        resp.Body.Close()
    
    }

    参考:https://www.cnblogs.com/liuhe688/p/10922881.html

    https://www.cnblogs.com/damir/archive/2012/05/06/2486663.html

  • 相关阅读:
    树的同构
    最大子列和
    多项式的表示和运算
    图1
    集合及运算
    树4
    树3
    树2
    期末作业验收
    个人总结
  • 原文地址:https://www.cnblogs.com/pu369/p/12859068.html
Copyright © 2011-2022 走看看