应用 Scrapy框架 ,配置动态IP处理反爬。
# settings 配置中间件 DOWNLOADER_MIDDLEWARES = { 'text.middlewares.TextDownloaderMiddleware': 543, # 'text.middlewares.RandomUserAgentMiddleware': 544, # 'text.middlewares.CheckUserAgentMiddleware': 545, 'text.middlewares.ProxyMiddleware': 546, 'text.middlewares.CheckProxyMiddleware': 547 } # settings 配置可用动态IP PROXIES = [ "http://101.231.104.82:80", "http://39.137.69.6:8080", "http://39.137.69.10:8080", "http://39.137.69.7:80", "http://39.137.77.66:8080", "http://117.191.11.102:80", "http://117.191.11.113:8080", "http://117.191.11.113:80", "http://120.210.219.103:8080", "http://120.210.219.104:80", "http://120.210.219.102:80", "http://119.41.236.180:8010", "http://117.191.11.80:8080" ]
# middlewares 配置中间件 import random class ProxyMiddleware(object): def process_request(self, request, spider): ip = random.choice(spider.settings.get('PROXIES')) print('测试IP:', ip) request.meta['proxy'] = ip class CheckProxyMiddleware(object): def process_response(self, request, response, spider): print('代理IP:', request.meta['proxy']) return response
到此这篇关于Scrapy 配置动态代理IP的实现的文章就介绍到这了,更多相关Scrapy 动态代理IP内容请搜索面圈教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持面圈教程!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:mianshige#126.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。