【问题标题】:How does one simulate the setting of a cookie by a javascript function in python mechanize?如何通过python mechanize中的javascript函数模拟cookie的设置?
【发布时间】:2013-12-08 00:11:09
【问题描述】:

我正在尝试使用 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,以便看起来我已经阅读并点击了警告?

【问题讨论】:

    标签: javascript python cookies mechanize-python


    【解决方案1】:

    您复制了 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")
    

    【讨论】:

    • 工作就像一个魅力。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多