2010-01-28 169 views
4

我认为机械化默认遵循重定向...通过我的脚本结束在重定向页面。我该如何处理?机械化和重定向(红宝石)

require 'rubygems' 
require 'mechanize' 

agent = WWW::Mechanize.new 

page = agent.get("http://www.vbulletin.org/forum/index.php") 

login_form = page.form_with(:action => 'login.php?do=login') 

login_form['vb_login_username'] = 'user name' 
login_form['vb_login_password'] = '' 
login_form['vb_login_md5password_utf'] = 'md5 hash from the password' 
login_form['vb_login_md5password'] = 'md5 hash from the password' 

page = agent.submit login_form 

#Display welcome message if logged in 
puts page.parser.xpath("/html/body/div/table/tr/td[2]/div/div").xpath('text()').to_s.strip 

output = File.open("login.html", "w") {|f| f.write(page.parser.to_html) } 

重定向页面的html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en"> 
<head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head> 
<body> 
<noscript> 

    <meta http-equiv="Refresh" content="2; URL=http://www.vbulletin.org/forum/index.php"> 
</noscript> 



    <script type="text/javascript"> 

    <!-- 

    function exec_refresh() 

    { 

     window.status = "Redirecting..." + myvar; 

     myvar = myvar + " ."; 

     var timerID = setTimeout("exec_refresh();", 100); 

     if (timeout > 0) 

     { 

      timeout -= 1; 

     } 

     else 

     { 

      clearTimeout(timerID); 

      window.status = ""; 

      window.location = "http://www.vbulletin.org/forum/index.php"; 

     } 

    } 



    var myvar = ""; 

    var timeout = 20; 

    exec_refresh(); 

    //--> 

    </script><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<meta name="generator" content="vBulletin 3.6.12"> 
<meta name="keywords" content="vbulletin,forum,bbs,discussion,jelsoft,bulletin board,hacks,modifications,addons,articles,programming,site review"> 
<meta name="description" content="This is a discussion forum powered by vBulletin. To find out about vBulletin, go to http://www.vbulletin.com/ ."> 
<!-- CSS Stylesheet --><link rel="stylesheet" type="text/css" href="clientscript/vbulletin_css/style-befeb917-00023.css" id="vbulletin_css"> 
<link rel="stylesheet" type="text/css" href="clientscript/blue.css" id="blue"> 
<!--/CSS Stylesheet --><script type="text/javascript"> 

<!-- 

var SESSIONURL = ""; 

var SECURITYTOKEN = "c1e3de2fd54e2938c4ab1e80ae448aa6bbea25b2"; 

var IMGDIR_MISC = "images/misc"; 

var vb_disable_ajax = parseInt("0", 10); 

// --> 

</script><script type="text/javascript" src="clientscript/vbulletin_global.js?v=3612"></script><link rel="alternate" type="application/rss+xml" title="vBulletin.org Forum RSS Feed" href="external.php?type=RSS2"> 
<!-- VB.ORG --><!-- The line above sets the script var and must be left in --><script type="text/javascript"> 

<!-- 

script = "login"; 

userid = "0"; 

forumid = ""; 

threadid = ""; 

authorid = ""; 

// --> 

</script><script type="text/javascript" src="clientscript/vborg_miscactions.js?v=3612"></script><!-- /VB.ORG --><title>vBulletin.org Forum</title> 
<div id="container" style="border-width:0;width:950px"> 

<br><br><br><br><form action="http://www.vbulletin.org/forum/index.php" method="post" name="postvarform"> 

<table class="tborder" cellpadding="4" cellspacing="1" border="0" align="center"> 
<tr> 
<td class="tcat">Redirecting...</td> 

</tr> 
<tr> 
<td class="panelsurround" align="center"> 

    <div class="panel"> 



     <blockquote> 

      <p> </p> 

      <p><strong>Thank you for logging in, my username.</strong></p>   



       <p class="smallfont"><a href="http://www.vbulletin.org/forum/index.php">Click here if your browser does not automatically redirect you.</a></p> 

       <div> </div> 



     </blockquote> 



    </div> 

    </td> 

</tr> 
</table> 
</form> 





<br><br><br><br> 
</div> 



</body> 
</html> 

回答

4

你确定它的HTTP重定向30X响应?不正常的“200 OK”响应页面包含<meta http-equiv="refresh" content="100;url=...">。 Mechanize遵循第一种类型的重定向,第二种是正常的链接响应,您应该遵循。

我无法检查,因为您没有在代码中发布用户名/密码。

编辑:
您的更新后,这应该是足够的跟随重定向登录后:

page.link_with(:text => "Click here if your browser does not automatically redirect you.").click 
+0

@MBO:我该如何检查它是什么类型?我不必在浏览器中点击任何内容,页面就会自动刷新。我在我的问题中添加了重定向页面html。我可以问你怎么知道什么类型的重定向机械化遵循? – Radek 2010-01-28 11:08:06

+0

@Radek我更新了我的回答 – MBO 2010-01-28 13:23:38

+0

@Radek你的另一种选择是在机械化后得到“http://www.vbulletin.org/forum/index.php”(或者说链接@MBO提及的href)之后你已被发送到此页面。 – 2010-01-28 18:34:32

39

有一个在机械化该选项。机械化默认情况下不遵循元刷新,但我们可以设置该选项。

agent.follow_meta_refresh = true 
+1

+1尼斯,我不知道那个选项 – MBO 2010-01-30 13:28:37

+0

niiiiccccceeeeee – Zombies 2010-02-25 18:17:00

+0

不错!我也不知道。 – 2010-11-19 17:25:06