使用浏览器登录,然后ruby/mechanize接管?

使用浏览器登录,然后ruby/mechanize接管?,ruby,mechanize,Ruby,Mechanize,这可能吗?我需要通过什么来实现机械化?我可以用什么网址开始呢 <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script> <table cellpadding="0" cellspacing="1" border="0"> <tr>

这可能吗?我需要通过什么来实现机械化?我可以用什么网址开始呢

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->
我无法(到目前为止)使用mechanize登录到一个网站,所以我在考虑是否可以做这个小的变通。我相信我可以捕获所有cookie和其他内容,然后将其传递给ruby/mechanize来完成其余的工作

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->
下面的屏幕截图是使用firebug()制作的

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->
有效登录=仅一行

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->
和html的登录工作

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->
无法工作的登录名的机械化日志

        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->
 INFO -- : Net::HTTP::Get: /login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1
DEBUG -- : request-header: accept-language => en-us,en;q=0.5
DEBUG -- : request-header: connection => keep-alive
DEBUG -- : request-header: accept => */*
DEBUG -- : request-header: accept-encoding => gzip,identity
DEBUG -- : request-header: user-agent => WWW-Mechanize/0.9.3 (http://rubyforge.org/projects/mechanize/)
DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
DEBUG -- : request-header: host => www.somedomain.com
DEBUG -- : request-header: keep-alive => 300
DEBUG -- : Read 400 bytes
DEBUG -- : Read 1424 bytes
DEBUG -- : Read 2448 bytes
DEBUG -- : Read 3211 bytes
DEBUG -- : response-header: vary => Accept-Encoding
DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
DEBUG -- : response-header: connection => close
DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
DEBUG -- : response-header: content-type => text/html; charset=utf-8
DEBUG -- : response-header: date => Fri, 29 Jan 2010 23:43:12 GMT
DEBUG -- : response-header: content-encoding => gzip
DEBUG -- : response-header: server => Apache/2.2.3 (CentOS)
DEBUG -- : response-header: content-length => 3211
DEBUG -- : response-header: set-cookie => PHPSESSID=7cfilg86ju2ldcgso22246hpu4; path=/, WebStats:visitorId=lSMkcwuSWEE%3D; expires=Mon, 27-Jan-2020 23:43:12 GMT; path=/, WebStats:sessionId=%2B2HHK296t%2BQ%3D; expires=Mon, 27-Jan-2020 23:43:12 GMT; path=/
DEBUG -- : response-header: accept-ranges => bytes
DEBUG -- : response-header: pragma => no-cache
DEBUG -- : gunzip body
DEBUG -- : saved cookie: PHPSESSID=7cfilg86ju2ldcgso22246hpu4
DEBUG -- : saved cookie: WebStats:visitorId=lSMkcwuSWEE%3D
DEBUG -- : saved cookie: WebStats:sessionId=%2B2HHK296t%2BQ%3D
 INFO -- : status: 200
DEBUG -- : query: "auth_username=radek&auth_password=mypassword"
 INFO -- : Net::HTTP::Post: /login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1
DEBUG -- : request-header: accept-language => en-us,en;q=0.5
DEBUG -- : request-header: connection => keep-alive
DEBUG -- : request-header: accept => */*
DEBUG -- : request-header: accept-encoding => gzip,identity
DEBUG -- : request-header: content-type => application/x-www-form-urlencoded
DEBUG -- : request-header: user-agent => WWW-Mechanize/0.9.3 (http://rubyforge.org/projects/mechanize/)
DEBUG -- : request-header: cookie => WebStats:sessionId=%2B2HHK296t%2BQ%3D; WebStats:visitorId=lSMkcwuSWEE%3D; PHPSESSID=7cfilg86ju2ldcgso22246hpu4
DEBUG -- : request-header: referer => http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1
DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
DEBUG -- : request-header: content-length => 43
DEBUG -- : request-header: host => www.somedomain.com
DEBUG -- : request-header: keep-alive => 300
DEBUG -- : Read 650 bytes
DEBUG -- : Read 1674 bytes
DEBUG -- : Read 2698 bytes
DEBUG -- : Read 3211 bytes
DEBUG -- : response-header: vary => Accept-Encoding
DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
DEBUG -- : response-header: connection => close
DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
DEBUG -- : response-header: content-type => text/html; charset=utf-8
DEBUG -- : response-header: date => Fri, 29 Jan 2010 23:43:13 GMT
DEBUG -- : response-header: content-encoding => gzip
DEBUG -- : response-header: server => Apache/2.2.3 (CentOS)
DEBUG -- : response-header: content-length => 3211
DEBUG -- : response-header: accept-ranges => bytes
DEBUG -- : response-header: pragma => no-cache
DEBUG -- : gunzip body
 INFO -- : status: 200

是的,捕获Cookie(例如,通过Firefox中的FireCookies插件)并手动将其传递给mechanize在大多数情况下都可以工作。

您的问题很可能是因为mechanize只跟踪使用设置的Cookie HTTP头创建的Cookie。它无法处理JavaScript创建的cookie。

@S.Mark:问题是我必须传递什么以及如何传递:-)请参见上图。您好,Firebug有FireCookie扩展,这是关于cookie选项卡的屏幕截图,您需要将这些cookie传递到mechanize@S.Mark:谢谢你。现在我在想下一步是什么。我从mechanize调用新url?但是哪一个呢?或者我在成功登录web浏览器后保存页面,然后将ruby scrip加载到
页面对象中
?对不起,请回答
如何将cookie传递到ruby/mechanize
的新问题,我在这方面做得不好。作为更新,FireCookie现在包含在Firebug插件中。@Radek,这是一个网络流吗?韦恩·康拉德:我更新了我的问题。我没有做任何不同的事情,只是在脚本不工作的情况下使用密码字段。在另一个例子中,我使用了密码的md5散列。我认为问题在于通信Web服务器浏览器不止一行,mechanize无法处理它。
        <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=3612"></script>
                    <table cellpadding="0" cellspacing="1" border="0">
                    <tr>
                        <td class="smallfont" align="left"><label for="navbar_username">User Name</label></td>
                        <td class="smallfont" align="left" colspan="2"><label for="navbar_password">Password</label></td>
                    </tr>
                    <tr>

                        <td><input type="text" class="button" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == 'User Name') this.value = '';" /></td>        
                        <td><input type="password" class="button" name="vb_login_password" id="navbar_password" size="10" accesskey="p" tabindex="102" /></td>
                        <td class="smallfont" align="left" valign="middle"><input type="submit" class="button" value="Log in" tabindex="103" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" />
                            <label for="cb_cookieuser_navbar">
                            <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Save?</label>
                            <input type="hidden" name="s" value="" />
<input type="hidden" name="securitytoken" value="1cbc0286417d97b4eb43ee0b0c2b54e7c615e5b8" />
                            <input type="hidden" name="do" value="login" />
                            <input type="hidden" name="vb_login_md5password" />

                            <input type="hidden" name="vb_login_md5password_utf" /></td>
                    </tr>
                    </table>
      </form>
      <!-- / login form -->