如何在scrapy中提出请求之前更改请求网址?

时间:2015-12-23 13:53:48

标签: python request scrapy

我需要在下载响应之前修改我的请求网址。但我无法改变它。即使在使用request.replace(url=new_url)修改请求网址后,process_response也会打印未修改的网址。这是中间件的代码:

def process_request(self, request, spider):
    original_url = request.url
    new_url= original_url + "hello%20world"
    print request.url            # This prints the original request url
    request=request.replace(url=new_url)
    print request.url            # This prints the modified url

def process_response(self, request, response, spider):
    print request.url            # This prints the original request url
    print response.url           # This prints the original request url
    return response

有谁能告诉我我在这里缺少什么?

1 个答案:

答案 0 :(得分:6)

由于您要修改request中的process_request()对象 - 您需要将其返回

def process_request(self, request, spider):
    request = request.replace(url=request.url + "hello%20world")
    return request