我需要在下载响应之前修改我的请求URL.但我无法改变它.即使在使用修改请求URL 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
谁能告诉我我在这里缺少什么?
由于您正在修改request
对象process_request()
- 您需要返回它:
def process_request(self, request, spider): request = request.replace(url=request.url + "hello%20world") return request