我有一个 python 脚本,它抓取页面并接收 cookie。我想将另一个 cookie 附加到发送到服务器的现有 cookie 中。这样,在下一个请求中,我将获得原始页面中的 cookie 以及我手动设置的 cookie。
无论如何要这样做吗?我尝试了机械化中的 addheaders 但它被忽略了。
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 ordinary
circumstances: cookie handling is normally entirely automatic. The
intended use case is rather to simulate the setting of a cookie by
client script in a web page (e.g. JavaScript). In that case, use of
this method is necessary because mechanize currently does not support
JavaScript, VBScript, etc.
The cookie is added in the same way as if it had arrived with the
current response, as a result of the current request. This means that,
for example, if it is not appropriate to set the cookie based on the
current request, no cookie will be set.
The cookie will be returned automatically with subsequent responses
made 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)