I have a python script which scrapes a page and receives a cookie. I want to append another cookie to the existing cookies that are being send to the server. So that on the next request I have the cookies from the original page plus ones I set manually.
Anyway of doing this? I tried addheaders in mechanize but it was ignored.
Use the set_cookie
method:
>>> import mechanize
>>> br=mechanize.Browser()>>> br.set_cookie?Definition: br.set_cookie(self, cookie_string)
Docstring:Request to set a cookie.Note that it is NOT necessary to call this method under ordinarycircumstances: cookie handling is normally entirely automatic. Theintended use case is rather to simulate the setting of a cookie byclient script in a web page (e.g. JavaScript). In that case, use ofthis method is necessary because mechanize currently does not supportJavaScript, VBScript, etc.The cookie is added in the same way as if it had arrived with thecurrent response, as a result of the current request. This means that,for example, if it is not appropriate to set the cookie based on thecurrent request, no cookie will be set.The cookie will be returned automatically with subsequent responsesmade by the Browser instance whenever that's appropriate.cookie_string should be a valid value of the Set-Cookie header.For example:browser.set_cookie("sid=abcdef; expires=Wednesday, 09-Nov-06 23:12:40 GMT")Currently, this method does not allow for adding RFC 2986 cookies.This limitation will be lifted if anybody requests it.