如何手动添加更多的Cookie到已经在机械化中设置了Cookie的会话? [英] How do I manually add more cookies to a session which already has cookies set in mechanize?

查看:369
本文介绍了如何手动添加更多的Cookie到已经在机械化中设置了Cookie的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,它抓取一个页面并接收一个cookie。我想将另一个Cookie附加到要发送到服务器的现有Cookie。所以在下一个请求,我有从原始页面的cookies加上手动设置的。

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.

这样做吗?我尝试了addheaders在机械化,但它被忽略。

Anyway of doing this? I tried addheaders in mechanize but it was ignored.

推荐答案

使用 set_cookie 方法:

>>> 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.

这篇关于如何手动添加更多的Cookie到已经在机械化中设置了Cookie的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆