python ip动态代理_Scrapy 配置动态代理IP的实现
應用 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內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
總結
以上是生活随笔為你收集整理的python ip动态代理_Scrapy 配置动态代理IP的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python包管理_Python包管理整
- 下一篇: 步步高vivo高通解锁工具_高通人工智能