ruby - Mechanize 和重定向( ruby )

标签 ruby rubygems mechanize

我认为 mechanize 默认遵循重定向......我的脚本在重定向页面结束。我该如何处理?

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>

最佳答案

Mechanize 中有一个选项可以做到这一点。默认情况下,Mechanize 不遵循元刷新,但我们可以设置该选项。

agent.follow_meta_refresh = true

关于ruby - Mechanize 和重定向( ruby ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2153763/

相关文章:

perl - 转到带有 mechanize-firefox 的 javascript 链接

ruby-on-rails - 无法将应用程序部署到 heroku

ruby - 在 Ruby 中写入文件的最佳方式是什么?

javascript - 通过 bootstrap 使用 jquery popover 进行 Rails CRUD

Windows bundle 安装 : HOME environment variable (or HOMEDRIVE and HOMEPATH) must be set and point to a directory (RuntimeError)

ruby - 如何隐藏或删除 gem 1.8.x 中的弃用通知?

ruby - 在 Mechanize 中使用 URI 方法#link

ruby - String.next 有反义词吗?

ruby-on-rails - 工头只显示 “started with pid #” 行,没有别的

python - 如何使用 Mechanize 捕捉超时