gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:44:06 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com