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



我正在使用机械化来尝试登录一个网站。但是,使用机械化登录时,我似乎无法到达最终目的地。下面是示例。

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

如果登录成功,网页将立即发布到下面的页面链接

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

相关内容

  • 没有找到相关文章

最新更新