我正在使用mechanize尝试登录网站。但是当使用mechanize登录时,我似乎无法进入最终目的地。以下是示例。
机械化去 (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>\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>
答案 0 :(得分:0)
看起来有一个元刷新(根据您的描述)。尝试将其添加到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