python使用爬虫代理的方案
2019-02-28 17:25
183 查看
scrapy中间件
在项目中新建middlewares.py文件(./项目名/middlewares.py)
#! -*- encoding:utf-8 -*- import base64 import sys import random PY3 = sys.version_info[0] >= 3 def base64ify(bytes_or_str): if PY3 and isinstance(bytes_or_str, str): input_bytes = bytes_or_str.encode('utf8') else: input_bytes = bytes_or_str output_bytes = base64.urlsafe_b64encode(input_bytes) if PY3: return output_bytes.decode('ascii') else: return output_bytes class ProxyMiddleware(object): def process_request(self, request, spider): # 代理服务器 proxyHost = "t.16yun.cn" proxyPort = "31111" # 代理隧道验证信息 proxyUser = "username" proxyPass = "password" request.meta['proxy'] = "http://{0}:{1}".format(proxyHost,proxyPort) # 添加验证头 encoded_user_pass = base64ify(proxyUser + ":" + proxyPass) request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass # 设置IP切换头(根据需求) tunnel = random.randint(1,10000) request.headers['Proxy-Tunnel'] = str(tunnel)
修改项目配置文件 (./项目名/settings.py)
DOWNLOADER_MIDDLEWARES = { 'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110, '项目名.middlewares.ProxyMiddleware': 100, }
相关文章推荐
- [python]新手写爬虫v2.5(使用代理的异步爬虫)
- Python3 爬虫使用User Agent和代理IP隐藏身份
- 使用PYTHON3写了一个简单爬虫, 通过公司代理爬取ppt素材
- python爬虫之Scrapy 使用代理配置
- JAVA使用爬虫代理的方案
- Python爬虫使用代理proxy抓取网页
- 鱼c笔记——Python爬虫(三):使用User Agent和代理IP隐藏身份
- Python爬虫之爬取——使用代理
- Python开发中爬虫使用代理proxy抓取网页的方法示例
- Python3 爬虫中代理的使用方法
- 如何使用http/https代理ip 纯手工浏览器(ie 360 谷歌 遨游等等)使用代理Ip 爬虫(python java)如何使用代理ip 附加免费代理ip池
- Python 爬虫入门(二)—— IP代理使用
- Python 爬虫入门—— IP代理使用
- 讲解Python的Scrapy爬虫框架使用代理进行采集的方法
- python3爬虫(8):异常处理以及代理的使用
- Python3爬虫之代理的使用
- python2-爬虫-系统环境变量读写-Handler和opener以及开放代理和私密代理的使用
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- 讲解Python的Scrapy爬虫框架使用代理进行采集的方法
- python爬虫之Scrapy 使用代理配置