一、使用開放代理(沒有用戶名和密碼)
# 開放代理
class IPProxyDownloadMiddleware():
PROXIES = ['175.42.68.217:9999',
'223.242.247.177:9999',
]
def process_request(self, request, spider):
proxy = random.choice(self.PROXIES)
request.meta['proxy'] = proxy
二、使用獨(dú)享代理(有用戶名和密碼)
# 獨(dú)享代理,需要用戶名和密碼
class IPProxyDownloadMiddleware(object):
def process_request(self, request, spider):
# 需要訪問的網(wǎng)站是http就寫http 如果是https就寫https,前提是代理本身具有相匹配的協(xié)議
proxy = 'http://112.74.198.237:16816'
user_password = "用戶名:密碼"
request.meta['proxy'] = proxy
# 轉(zhuǎn)成bytes
b64_user_password = base64.b64encode(user_password.encode('utf-8'))
# Basic后面要有空格
request.headers['Proxy-Authorization'] = 'Basic ' + b64_user_password.decode('utf-8')
三、setting設(shè)置
開啟下載中間件