gpt4 book ai didi

ruby - Mechanize cookie_jar 不保存 session token

转载 作者:数据小太阳 更新时间:2023-10-29 07:27:13 24 4
gpt4 key购买 nike

我正在根据用户的请求使用 Mechanize 抓取受密码保护的网站。我试图通过运行 Rake 任务来分离登录和搜索功能,该任务登录到站点并将 cookie 保存到数据库中,该数据库由后续的 Mechanize 请求重用。

我的问题是 cookie_jar.save 方法不会将 session cookie/ token 保存到 cookie 文件中。这里有一个简单的例子来证明这一点:

require 'mechanize'

# Setup Mechanize agents
agent1 = Mechanize.new
agent2 = Mechanize.new

# Fetch page and save cookies to local file
agent1.get ('http://www.my-secure-website.com')
agent1.post('http://www.my-secure-website.com/login', {
'user[login]' => 'my_login',
'user[password]' => 'my_password',
'submit' => 'Login'
})

# Verify and save cookies
agent1.cookie_jar.save_as 'cookies'
p agent1.cookie_jar
# #<Mechanize::CookieJar:0x8cf60b8 @jar={"www.my-secure-website.com"=>{"/"=>{"JSESSIONID"=>JSESSIONID=1NqLRc4dm0Qp5465N82Zwz4N0yXxy5jP1pXpyKp9jG8ssX2nMp5q!-334818122}, "/login/"=>{"Account"=>Account=my_account_number}}, "evr.my-secure-website.com"=>{"/APBDBQ"=>{"JSESSIONID"=>JSESSIONID=A74D230DEAFF50098557FBE76DD2E0C5}}}


########################################################
# Now let's load cookies into the second Mechanize agent

# Version 1 - This works only partially. Session cookies are missing:
agent2.cookie_jar.load 'cookies'
p agent2.cookies
# [
# [0] Account=my_account_number
# ]
p agent2.cookie_jar
# #<Mechanize::CookieJar:0x914c658 @jar={"www.my-secure-website.com"=>{"/"=>{}, "/login/"=>{"Account"=>Account=my_account_number}}, "evr.my-secure-website.com"=>{"/APBDBQ"=>{}}}>


# Version 2 - This works, but cannot be saved into file/db!
agent2.cookie_jar = agent1.cookie_jar
p agent2.cookies
# [
# [0] JSESSIONID=1NqLRc4dm0Qp5465N82Zwz4N0yXxy5jP1pXpyKp9jG8ssX2nMp5q!-334818122,
# [1] Account=my_account_number,
# [2] JSESSIONID=A74D230DEAFF50098557FBE76DD2E0C5
# ]
p agent2.cookie_jar
# #<Mechanize::CookieJar:0x8cf60b8 @jar={"www.my-secure-website.com"=>{"/"=>{"JSESSIONID"=>JSESSIONID=1NqLRc4dm0Qp5465N82Zwz4N0yXxy5jP1pXpyKp9jG8ssX2nMp5q!-334818122}, "/login/"=>{"Account"=>Account=my_account_number}}, "evr.my-secure-website.com"=>{"/APBDBQ"=>{"JSESSIONID"=>JSESSIONID=A74D230DEAFF50098557FBE76DD2E0C5}}}>

这是我保存的 cookie 文件的样子:

---
www.my-secure-website.com:
/: {}
/login/:
Account: !ruby/object:Mechanize::Cookie
version: 0
port:
discard:
comment_url:
expires: Thu, 22 May 2014 07:48:46 GMT
max_age:
comment:
secure: true
path: /login/
domain: www.my-secure-website.com
accessed_at: 2013-05-22 00:48:47.227628764 -07:00
created_at: 2013-05-22 00:48:47.227628764 -07:00
name: Account
value: S4633
for_domain: false
domain_name: !ruby/object:DomainName
ipaddr:
hostname: www.my-secure-website.com
uri_host: www.my-secure-website.com
tld: com
canonical_tld_p: true
domain: my-secure-website.com
session: false
evr.my-secure-website.com:
/APBDBQ: {}

您可以在控制台输出中看到 session token (JSESSIONID),但它在本地 cookie 文件中缺失。我的问题是,如何让 Mechanize.cookie_jar.save_as 也保存 session 数据?

最佳答案

从 Mechanize 版本 2.6.0 开始。 cookie_jar.save_as 方法允许传递 :session 选项以启用保存 session cookie:

agent1.cookie_jar.save_as 'cookies', :session => true, :format => :yaml

附言我使用的是 v2.5.1,它缺少此功能。

关于ruby - Mechanize cookie_jar 不保存 session token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16686572/

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