механизировать и перенаправить (ruby)

Я думал, что 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>

person Radek    schedule 28.01.2010    source источник


Ответы (2)


Вы уверены, что это ответ HTTP 30x Redirect? Ненормальный ответ "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
person MBO    schedule 28.01.2010
comment
@MBO: как я могу проверить, какой это тип? Мне не нужно ничего нажимать в браузере, страница обновляется сама. Я добавил html страницы перенаправления в свой вопрос. Могу я спросить, откуда вы знаете, какой тип механизма перенаправления следует? - person Radek; 28.01.2010
comment
@Radek, другой вариант — просто заставить механизировать vbulletin.org/forum/index.php (точнее, href ссылки, упомянутой @MBO) после того, как вы были отправлены на эту страницу. - person Tim Snowhite; 28.01.2010
comment
это точно способ, как это решить... я нашел еще кое-что. Смотри мой ответ - person Radek; 30.01.2010

Для этого есть опция в механизации. По умолчанию Mechanize не следует мета-обновлениям, но мы можем установить эту опцию.

agent.follow_meta_refresh = true
person Radek    schedule 30.01.2010
comment
Хороший! Я тоже не знал об этом. - person Tim Snowhite; 19.11.2010