接続法 私たちのスパイダーはリンクをたどり、アイテムを返す「ページの解析」関数でそれらを解析します。parse_pageを最初に呼び出す前に、各リクエストに異なるプロキシを追加するにはどうすればよいですか?
たとえば、250のプロキシのプールがあり、リクエストに対してそれぞれをランダムに選択したいとします。
このためのミドルウェアを作成できます。例えば:
#Start your middleware class
class ProxyMiddleware(object):
# overwrite process request
def process_request(self, request, spider):
# Set the location of the proxy
request.meta['proxy'] = "http://123.456.789.012"
# Use the following lines if your proxy requires authentication
proxy_user_pass = "USER_AND_PASS"
# setup basic authentication for the proxy
encoded_user_pass = base64.encodestring(proxy_user_pass)
request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
上記のコードを変更することで、プロキシのURL、ユーザー名、パスワードを簡単にランダム化できると思います。さらにサポートが必要な場合はお知らせください。