經歷了架不起來的 tor, gapproxy. 我快崩潰了。
只要有可以用的 proxy server,就好了!
1.新增 /project/app/middlewares.py
設定proxy的server、帳號、密碼
#-*- encoding: utf-8 -*-
import base64
class ProxyMiddleware(object):
def process_request(self, request, spider):
request.meta['proxy'] = "http://PROXYSERVER:PORT"
proxy_user_pass = "USERNAME:PASSWORD"
encoded_user_pass = base64.encodestring(proxy_user_pass)
request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
import base64
class ProxyMiddleware(object):
def process_request(self, request, spider):
request.meta['proxy'] = "http://PROXYSERVER:PORT"
proxy_user_pass = "USERNAME:PASSWORD"
encoded_user_pass = base64.encodestring(proxy_user_pass)
request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
2.設定檔 settings.py
DOWNLOADER_MIDDLEWARES = {
'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110,
'APPNAME.middlewares.ProxyMiddleware': 100,
}
'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110,
'APPNAME.middlewares.ProxyMiddleware': 100,
}
0 留言