有什么问题这个Ruby机械化登录stackoverflow脚本? - What's wrong with this Ruby Mechanize login to stackoverflow script?

- 此内容更新于:2016-02-02
主题:

这有什么问题?这让我过去第一个登录页面(我认为),但我似乎仍然注销。

原文:

What is wrong with this? This gets me past the first login page (I think), but I appear to remain logged out.

require 'rubygems'
require 'mechanize'

#create agent
agent = Mechanize.new { |agent| 
    agent.user_agent_alias = 'Mac Safari 4'
}
agent.follow_meta_refresh = true
page = agent.get("https://stackoverflow.com/users/login")

#get login form
login_form = page.form_with(:action => "/users/login")

login_form.openid_username = "username"
login_form.openid_identifier = "password"
page = agent.submit(login_form)
#pp page
解决方案:
这是如果你想登录用户名/密码。
原文:
require 'mechanize'

Mechanize.new do |agent|
  agent.user_agent_alias = 'Mac Safari'
  agent.get("https://stackoverflow.com/users/login")
  agent.page.forms.last do |f|
    f.email = "username"
    f.password = "password"
    f.submit
  end
end

This works if you want to login with username/password.

网友:不是吗?agent.page.uri。to_s==stackoverflow.com/users/login相同的结果我从源自代码。

(原文:Does it? agent.page.uri.to_s == stackoverflow.com/users/login which is the same result I get from the orig code.)

网友:你有一个有效的用户名/密码登录进行测试?尝试使用,看看它的工作原理。

(原文:Do you have a valid username/password to login for testing? Try to use agent.get(some_new_uri) to see if it works.)