如何手动向已在 mechanize 中设置了 cookie 的会话添加更多 cookie?

2024-04-01

我有一个 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(使用前将#替换为@)

如何手动向已在 mechanize 中设置了 cookie 的会话添加更多 cookie? 的相关文章

随机推荐

  • 查找“IN”子句中与记录不匹配的值

    我有一个dept表与eng and finance记录 它不包含以下记录foo bar or baz 我有一个休眠查询 它返回与我要查找的内容匹配的记录 看起来像 select from dept where dept name in en
  • 布局权重在 ScrollView 中不起作用

    我想将布局权重分配给一个中的多个项目LinearLayout里面的一个ScrollView 但是 那ScrollView忽略LinearLayout weightSum 我的目标是将布局划分为权重 2 1 1 总和为 4 但这在 a 内部无
  • 在 R 中访问多个艺术家的 Spotify API

    我在 Spotify 的开发者应用程序部分创建了客户端 ID 和密钥 我正在参考这份文件 并希望通过选择多名艺术家来扩展它 https www r bloggers com the eurovision 2016 song contest
  • 使用指针交换对象

    我正在尝试为使用 void 指针交换对象的作业问题交换对象 我的函数的声明必须是 void swap void a void b size t size 我并不是在寻找如何执行此操作的确切代码 这样我就可以自己弄清楚 但我不确定我是否理解正
  • 找出3个字母的单词

    我的 Python 代码如下 import re string what are you doing you i just said hello guys regexValue re compile r s w w w s mo regex
  • R光栅绘制图像,画一个圆并掩盖圆外的像素

    下面的代码绘制一个图像 然后在该图像上绘制圆圈 我想让该圆圈之外的所有像素都变成黑色 我怎么能这么做呢 library raster library plotrix r1 lt brick system file external rlog
  • 无法安装 XML 包

    我无法安装最新的 XML 包 V3 9 4 我通过键入以下内容在 RGUI 中手动安装它 install packages LOCALDIR XML 3 6 0 tar gz repos NULL type source 但收到一些错误消息
  • Vaadin 7 在执行后台线程后不刷新 UI(仅当需要超过 5 分钟时)

    我正在使用 Vaadin 7 在我的应用程序中 有一个表格列表 用户可以选择一个或多个项目来详细说明 如果单击执行按钮 UI 组件将被禁用并启动两个线程 第一个线程执行繁重的计算 第二个线程更新 UI 上的进度指示器 使用 lock 和un
  • jest.mock express-jwt 受保护路由的中间件行为

    所以现在我有这样的东西 这不起作用 import app from src app beforeAll gt jest mock src middleware auth gt req Request res Response next Ne
  • 为什么在 initWIthCoder:(NSCoder *)aDecoder 中调用时未设置我的 UIView 图层属性?

    我构建了一个自定义 View 类 它是 UIView 的子类 该视图通过 ViewController 中的 nib 文件加载 NSBundle main loadNibNamed MyCustomView owner self optio
  • CUnit - “模拟”libc 函数

    我正在使用 CUnit 进行项目单元测试 我需要测试是否使用正确的参数调用 libc 函数以及是否以正确的方式处理它们的返回值 例如 如果我调用 bind 函数 我想检查我传递的 af 参数并断言如果这是错误的 并且我还想模拟它的返回值并断
  • Google OAuth2 授权 OAuth 令牌错误:redirect_uri_mismatch

    我正在根据此网页创建并授权 OAuth 令牌 https code google com p google mail oauth2 tools wiki OAuth2DotPyRunThrough https code google com
  • 找到最小正值

    从固定数量 在本例中为 3 个值中找到最小非零正值或在没有正问题时返回 0 的最佳算法是什么 我的天真的方法如下 在Delphi中 但请随意使用您喜欢的任何方法 但我认为有一种更优雅的方法 value1Temp MaxInt value2T
  • 将多个源 ArrayList 同步到单个目标列表

    需要从两个不同的表加载项目列表 ArrayList 由于 Hibernate 映射复杂 它们无法作为一个 ArrayList 加载 所以它们是由类定义中的两个不同的Hibernate包分别加载的 当我使用它们时 我想合并成一个列表 所以我正
  • 仅在调试模式下使用特定的 minSdkVersion

    如何仅在调试模式下使用特定的 minSdkVersion 我想使用 minSdkVersion 21 进行调试模式 但使用 minSdkVersion 15 进行发布 我不想为此使用香料 因为会带来麻烦 我认为可能是这样的 但不起作用 de
  • 你能在 Perl 中强制刷新输出吗?

    我在 Perl 中有以下两行 print Warning this will overwrite existing files Continue y N n my input
  • 将 SAFEARRAY 从 C++ 返回到 C#

    我有一个 C 方法 可以创建 填充并返回 SAFEARRAY SAFEARRAY TestClass GetResult long size return GetSafeArrayList size How should I export
  • Lua中运算符~=是什么意思?

    什么是 Lua中的运算符是什么意思 例如 在以下代码中 if x params then the is not equals 这在其他语言中是等价的
  • 尝试在Flash AS3.0中使用BindingUtils

    我无法使此代码在包含 Flex SDK 4 0 的 AS3 0 Flash 中工作 import mx binding utils Bindable var myValue int 0 var cw ChangeWatcher Bindin
  • 如何手动向已在 mechanize 中设置了 cookie 的会话添加更多 cookie?

    我有一个 python 脚本 它抓取页面并接收 cookie 我想将另一个 cookie 附加到发送到服务器的现有 cookie 中 这样 在下一个请求中 我将获得原始页面中的 cookie 以及我手动设置的 cookie 无论如何要这样做