javascript - 如何通过 python mechanize 中的 javascript 函数模拟 cookie 的设置?

标签 javascript python cookies mechanize-python

我正在尝试使用 python mechanize 登录并逐步浏览一个网站,该网站的初始页面有一个警告,必须单击并确认才能继续登录页面。单击该链接会激活一个 javascript 函数,该函数会设置一个 cookie 并将您发送到登录页面。

警告的 html 是

<a id='has-seen-warning' class='button' href='/login/'>I Agree</a>

设置 cookie 的 javascript 如下:

<script>
    $(function(){
        var agreed_to_cookie = 'agreed_to_notice';
        $('#has-seen-warning').click(function(){
            $.cookie(agreed_to_cookie, 'True', {expires: 365, path:'/', secure: true});
        });
    });
</script>

从示意图上看,我的 python 代码如下所示:

import mechanize
br = mechanize.Browser()
url = "https:/blah.blah/login"
# Call login page first time
br.open(url)
# but get request to agree to notice.
# Set the cookie
br.set_cookie("agreed_to_cookie=True; expires=Sunday, 08-Dec-13 23:12:40 GMT")

# call the log in page again
br.open(url)

我应该如何设置 cookie,以显示我已阅读并点击了警告?

最佳答案

您错误地复制了 Set-Cookie 字符串。试试这个

// TODO: replace expiration date with your own
br.set_cookie("agreed_to_notice=True; path=/; expires=Sunday, 08-Dec-14 23:12:40 GMT; secure")

关于javascript - 如何通过 python mechanize 中的 javascript 函数模拟 cookie 的设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448322/

相关文章:

python - 哪个更基本 : Python functions or Python object-methods?

python - 找不到 _iconv 的 pyspatialite 符号

python - django 中需要 csrftoken cookie 和 csrf_token INPUT 类型吗

javascript - JS Math setinterval 一个范围内的随机数

带有 ssl 的 Python Gevent Pywsgi 服务器

java - 从 servlet API 设置时是否需要转义 cookie 值?

ios - SFSafariViewController cookies

javascript - 如何为文本框设置按钮渐变颜色?

javascript - 如何获取 Kendo 工具栏项的数据源?

javascript - jQuery有collect方法吗