机械化:无法重定向到最终目的地

Mechanize: Unable to redirect to final destination

我正在使用 mechanize 尝试登录网站。但是,当使用 mechanize 登录时,我似乎无法到达最终目的地。下面是示例。

机械化去 (1) https://app.abc.com/users/login

如果登录成功,网页会暂时post到下面link页

(2) https://paid.abc.com/Login.aspx?CID=123&XRALID=321&LanguageID=1&ExecuteLogin=1

从那里自动重定向到最终目的地

(3) https://paid.abc.com/ABC/ABCMainStrict.aspx?state=SearchQueriesStrict

我的代码如下:

require 'mechanize'

def say(msg)
  puts '- ' + msg
end

def exit_with(msg)
  say msg
  puts "...exiting..."
  exit
end

a = Mechanize.new

a.user_agent = "Friendly Mechanize Script"
a.redirect_ok = true

say "Logging in..."
a.get('https://app.abc.com/users/login') do |page|
  content_page = page.form_with(:action => "/users/login") do |f|
    username_field = f.field_with(name: "UserName")
    username_field.value = 'testing@example.com'
    password_field = f.field_with(name: "Password")
    password_field.value = 'Password123'
  end.submit

  say "Got page: " + content_page.title
  say "Show page: " + content_page.uri.to_s
  new_link = content_page.uri.to_s

  exit_with("Couldn't log in.") if content_page.uri.to_s =~ /login/
  puts a.page.inspect

end

"a.page.inspect"的输出如下

#<Mechanize::Page
 {url
  #<URI::HTTPS:xxx URL:https://paid.abc.com/Login.aspx?CID=123&XRALID=321&LanguageID=1&ExecuteLogin=1>}
 {meta_refresh}
 {title "ABC RELATIONS"}
 {iframes}
 {frames}
 {links
  #<Mechanize::Page::Link
   "Internet Explorer 8.0 or higher"
   "http://www.microsoft.com/ie">
  #<Mechanize::Page::Link "Firefox 3.6 or higher" "http://www.getfirefox.com">
  #<Mechanize::Page::Link
   "Chrome 13.0 or higher"
   "http://www.google.com/chrome">
  #<Mechanize::Page::Link
   "Safari 5.0 or higher"
   "http://www.apple.com/safari/">}
 {forms}>

更新

感谢@JonB,我意识到我正在尝试做的事情叫做跟随元刷新。我认为下面的这段代码有助于解决这个问题。

<head>\r\n    
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\r\n    
<title>ABC RELATIONS</title>\r\n    
<script src=\"Login.js\" type=\"text/javascript\" />\r\n    
<link rel=\"shortcut icon\" href=\"favicon.ico\" type=\"image/x-icon\" />\r\n    
<script type=\"text/javascript\" src=\"//use.typekit.net/qsx7ypf.js\" />\r\n    
<script type=\"text/javascript\">try{Typekit.load();}catch(e){}</script>\r\n    
<style type=\"text/css\">\r\n\t\t\t\t\t.clearfix {display: inline-block;}  /* for IE/Mac */\r\n\t\t\t\t</style>\r\n    
<link href=\"Images/Login/styles4.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n  
<script type=\"text/javascript\">var autoLogin = false;</script>
</head>

看起来那里有元刷新(根据您的描述)。尝试将此添加到您的 Mechanize 对象:

a.follow_meta_refresh = true

此外,您可能希望 user_agent 为可接受的值而不是自定义值:

require 'mechanize'
Mechanize::AGENT_ALIASES.each { |k,v| puts k }
=> Mechanize
=> Linux Firefox
=> Linux Konqueror
=> Linux Mozilla
=> Mac Firefox
=> Mac Mozilla
=> Mac Safari 4
=> Mac Safari
=> Windows Chrome
=> Windows IE 6
=> Windows IE 7
=> Windows IE 8
=> Windows IE 9
=> Windows Mozilla
=> iPhone
=> iPad
=> Android
=> Mac FireFox
=> Linux FireFox