1

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

4

1 回答 1

2

您复制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")
于 2013-12-09T16:31:47.573 回答