I am trying to access a session cookie inside a spider. First I enter the social network using in a spider:
def parse(self, response): return [FormRequest.from_response(response, formname='login_form', formdata={'email': '...', 'pass':'...'}, callback=self.after_login)]
In after_login I would like to access session cookies in order to transfer them to another module (selenium here) in order to continue processing the page using an authenticated session.
I would like something like this:
def after_login(self, response):
Unfortunately, response.cookies does not return session cookies.
How can I get session cookies? I looked at the cookie middleware: scrapy.contrib.downloadermiddleware.cookies and scrapy.http.cookies, but there seems to be no easy way to access session cookies.
More details are given in my original question:
Unfortunately, I used your idea, but I donβt see cookies, although I know for sure that they exist because scrapy.contrib.downloadermiddleware.cookies prints cookies! These are the cookies I want to capture.
So here is what I am doing:
The after_login (self, response) method gets the response variable after proper authentication, and then I access the URL with the session data:
def after_login(self, response): # testing to see if I can get the session cookies cookieJar = response.meta.setdefault('cookie_jar', CookieJar()) cookieJar.extract_cookies(response, response.request) cookies_test = cookieJar._cookies print "cookies - test:",cookies_test # URL access with authenticated session url = "http://site.org/?id=XXXX" request = Request(url=url,callback=self.get_pict) return [request]
As you can see from the output below, there really are cookies, but I canβt grab them using cookieJar:
cookies - test: {} 2012-01-02 22:44:39-0800 [myspider] DEBUG: Sending cookies to: <GET http://www.facebook.com/profile.php?id=529907453> Cookie: xxx=3..........; yyy=34.............; zzz=.................; uuu=44..........
So, I would like to get a dictionary containing the keys xxx, yyy, etc. with corresponding values.
Thank:)