How to get cookie from scrapy response and set the cookie to the next request?

I have disabled the Default Scrapy cookie option, so that i have to set it manually.

COOKIES_ENABLED = False
COOKIES_DEBUG = True

Now, i need to set cookie with the value which is received as the response of the same site. I can able to get the cookie as below,

cookie = response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")

now i am trying to set it to the form request by

FormRequest.from_response(response,
                formdata={"username": "asldkfs", "pass": "slskd"},
                cookies={cookie[0]:cookie[1]},
                meta = {'dont_redirect': True,'handle_httpstatus_list': [302]},
                callback=self.redirection)

def redirection(self,response): 
    self.log("redirection")
    self.log(response.headers)               
    self.log("Cookie2")
    cook1 = response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")
    self.log(cook1)        
    self.log("end cookie2")
    return Request("http://something.net/some/sa/"+response.headers.getlist('Location')[0],cookies={cook1[0]:cook1[1]},
        callback=self.check_login_response)

.
.
.

So I could not set the cookie.Do i need to set any other value also or what could be the problem?


The cookies argument only works if you have COOKIES_ENABLED set to True, since CookiesMiddleware handles it.

Hence you have to set it manually on the headers:

cookie = response.headers.getlist('Set-Cookie')[0].split(';')[0]

FormRequest.from_response(response,
            formdata={"username": "asldkfs", "pass": "slskd"},
            headers={'Cookie': cookie}, # <---
            meta = {'dont_redirect': True,'handle_httpstatus_list': [302]},
            callback=self.redirection)

如果您禁用了Cookie,我认为您无法使用Cookie。

链接地址: http://www.djcxy.com/p/33260.html

上一篇: 如何将Luis钩入Bot框架FormDialog

下一篇: 如何从scrapy响应获取cookie并将cookie设置为下一个请求?