互联网的发展,大数据时代的来临,越来越多的网络爬虫兴起。网络爬虫使用HTTP代理也一种常见的状态。HTTP代理分为许多类型,动态短效IP,优质代理,爬虫代理加强版, 长效代理等等。爬虫的业务不同所需要的不同类型的HTTP代理自然也不同。但是网络爬虫使用HTTP代理应该注意哪些问题呢?
网络爬虫的业务就是数据采集,做过爬虫业务的用户都清楚,如果采集的速度过快,就会被网站限制,导致出现封IP封爬虫行为等等。每个网站基本上都会有反爬策略。因为爬虫在采集数据的时候,会快速大量的发出请求,影响了正常用户的访问和运行。所以网站都会设置反爬机制这种功能。如果网络爬虫要顺利的采集数据业务,就需要代理触发网站的反爬机制前,替换一个新IP模拟一个真实用户去访问,这样网络爬虫采集正常的采集到想要的数据信息。
当然网络爬虫在使用HTTP代理的时候,需要使用高质量的高匿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)
}
}