爬虫代理适合的业务和场景

时间:2022-07-27
本文章向大家介绍爬虫代理适合的业务和场景,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

网络爬虫中崛起,越来越多的人使用爬虫代理。爬虫代理是网络爬虫不可缺少的一部分,那爬虫代理适合什么业务或者什么场景呢?

理服务器的作用便是协助互联网客户代理获得网络信息,就网络信息的转运站。假如要想提升访问权限,代理服务器能够协助你,很多人必须掩藏 IP地址 ,爬虫代理就能配合。

爬虫虫代理适合的业务类型:数据采集、seo优化、游戏注册、刷量、点击、投票等各种业务和场景

互联网许多情况下都可以应用到 代理ip ,非常是一些互联网营销有关的工作中,代理ip是十分必需的,挑选付钱的高品质代理ip是更为适合的。

使用爬虫代理

        package main

        import (
            "net/url"
            "net/http"
            "bytes"
            "fmt"
            "io/ioutil"
        )

        // 代理服务器(产品官网 www.16yun.cn)
        const ProxyServer = "t.16yun.cn:31111"

        type ProxyAuth struct {
            Username string
            Password string
        }

        func (p ProxyAuth) ProxyClient() http.Client {

            var proxyURL *url.URL
            if p.Username != ""&& p.Password!="" {
                proxyURL, _ = url.Parse("http://" + p.Username + ":" + p.Password + "@" + ProxyServer)
            }else{
                proxyURL, _ = url.Parse("http://" + ProxyServer)
            }
            return http.Client{Transport: &http.Transport{Proxy:http.ProxyURL(proxyURL)}}
        }

        func main()  {


            targetURI := "https://httpbin.org/ip"


            // 初始化 proxy http client
            client := ProxyAuth{"username",  "password"}.ProxyClient()

            request, _ := http.NewRequest("GET", targetURI, bytes.NewBuffer([] byte(``)))

            // 设置Proxy-Tunnel
            // rand.Seed(time.Now().UnixNano())
            // tunnel := rand.Intn(10000)
            // request.Header.Set("Proxy-Tunnel", strconv.Itoa(tunnel) )

            response, err := client.Do(request)

            if err != nil {
                panic("failed to connect: " + err.Error())
            } else {
                bodyByte, err := ioutil.ReadAll(response.Body)
                if err != nil {
                    fmt.Println("读取 Body 时出错", err)
                    return
                }
                response.Body.Close()

                body := string(bodyByte)

                fmt.Println("Response Status:", response.Status)
                fmt.Println("Response Header:", response.Header)
                fmt.Println("Response Body:n", body)
            }
        }