[筆記] scrapy & proxy

搞個老半天根本不用自己架 proxy server.

經歷了架不起來的 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

2.設定檔 settings.py


DOWNLOADER_MIDDLEWARES = {
    'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110,
    'APPNAME.middlewares.ProxyMiddleware': 100,
}

張貼留言

0 留言