浏览 411 次
锁定老帖子 主题:通过搭建squid实现爬虫需求
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2021-05-26
这个网站记录了各国的接种数据信息,我们就通过https://ourworldindata.org/covid-vaccinations来了解下最新的各国接种疫苗信息。 我们可以用Proxy-Tunnel来控制代理IP,在HTTPS的情况下,还可以直接通过TCP链接断开的方式来控制代理IP,更多例子可以参考 https://www.16yun.cn/help/ss_demo/ 完整代码如下: #! -*- encoding:utf-8 -*- import requests import random import requests.adapters # 要访问的目标页面 targetUrlList = [ "https://ourworldindata.org/covid-vaccinations", ] # 代理服务器(产品官网 www.16yun.cn) proxyHost = "localhost" proxyPort = "3128" proxyMeta = "http://%(host)s:%(port)s" % { "host": proxyHost, "port": proxyPort, } # 设置 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 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |