python – 使用scrapy-splash代理

我正在尝试使用代理(proxymesh)和scrapy-splash.我有以下(相关)代码

PROXY = """splash:on_request(function(request)
    request:set_proxy{
        host = http://us-ny.proxymesh.com,
        port = 31280,
        username = username,
        password = secretpass,
    }
    return splash:html()
end)"""

并在start_requests中

def start_requests(self):
    for url in self.start_urls:
        print url
        yield SplashRequest(url, self.parse,
            endpoint='execute',
            args={'wait': 5,
                  'lua_source': PROXY,
                  'js_source': 'document.body'},

但它似乎没有用. self.parse完全没有被调用.如果我将端点更改为’render.html’,我会点击self.parse方法,但是当我检查标题(response.headers)时,我可以看到它不会通过代理.我确认当我将http://checkip.dyndns.org/设置为起始URL并在解析响应时看到我的旧IP地址.

我究竟做错了什么?

最佳答案 您应该向SplashRequest对象添加’proxy’参数.

def start_requests(self):
    for url in self.start_urls:
        print url
        yield SplashRequest(url, self.parse,
            endpoint='execute',
            args={'wait': 5,
                  'lua_source': PROXY,
                  'js_source': 'document.body',
                  'proxy': 'http://proxy_ip:proxy_port'}
点赞