了解了使用亿牛云的用户其实都很了解,一些免费的代理IP实际上与白名单绑定没有多大关系,还有一些普通的低价格代理,或许开放类的代理都不需要授权就可以直接使用。但是这种代理IP的使用效果并不理想,一般高匿优质代理都需要绑定白名单,进行双向绑定,提取和使用代理的机器IP都必须在白名单内,才能正常使用代理,进行业务才能使业务成功率达到最高效果。
在我们现在的日常生活中离不开网络,在网络生活我我们会使用到代理ip。网络上有很多的代理ip可以选择,操作都是比较简单的,能够非常方便得进行使用。
代理也分很多种,传统的API代理,隧道转发的爬虫代理等等。一般网上都会有这种代理使用方式教程,好的一点代理商,传统的API代理和隧道转发的爬虫代理都是支持的。
传统API提取式代理,通过URL定时获取代理IP信息,需验证IP的可用性、更换代理设置,同时需要设计多线程异步IO,实现代理IP并发处理,不仅繁琐,而且影响效率。
“亿牛云爬虫代理IP”通过固定云代理服务地址,建立专线网络链接,代理平台自动实现毫秒级代理IP切换,保证了网络稳定性和速度,避免爬虫客户在代理IP策略优化上投入精力。
网络爬虫或者企业公司无论选择哪种代理使用方式,都需要参考自己的接口是否支持,如果不支持可以根据每个代码demo去调整修改。
#! -*- encoding:utf-8 -*-
import requests
import random
import requests.adapters
# 要访问的目标页面
targetUrlList = [
"https://httpbin.org/ip",
"https://httpbin.org/headers",
"https://httpbin.org/user-agent",
]
# 代理服务器(产品官网 www.16yun.cn)
proxyHost = "t.16yun.cn"
proxyPort = "31111"
# 代理验证信息
proxyUser = "username"
proxyPass = "password"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host": proxyHost,
"port": proxyPort,
"user": proxyUser,
"pass": proxyPass,
}
# 设置 http和https访问都是用HTTP代理
proxies = {
"http": proxyMeta,
"https": proxyMeta,
}
# 设置IP切换头
tunnel = random.randint(1, 10000)
headers = {"Proxy-Tunnel": str(tunnel)}
class HTTPAdapter(requests.adapters.HTTPAdapter):
def proxy_headers(self, proxy):
headers = super(HTTPAdapter, self).proxy_headers(proxy)
if hasattr(self, 'tunnel'):
headers['Proxy-Tunnel'] = self.tunnel
return headers
# 访问三次网站,使用相同的tunnel标志,均能够保持相同的外网IP
for i in range(3):
s = requests.session()
a = HTTPAdapter()
# 设置IP切换头
a.tunnel = tunnel
s.mount('https://', a)
for url in targetUrlList:
r = s.get(url, proxies=proxies)
print r.text