2010-01-30 42 views
4

這甚至可能嗎?我需要通過機械化?我可以從哪些網址開始呢?用瀏覽器登錄,然後ruby/mechanize接管它?

我無法管理(迄今)使用機械化登錄到一個網站,所以我想如果我可以做這個小解決方法。我相信我可以捕獲所有的cookies和其他一切,然後把它傳遞到紅寶石/機械化做休息......

截圖下面是使用螢火蟲(Firebug logs the POST or GET request, the response headers

登錄,它的工作原理=只有一行

製造

login that works = just one line http://i45.tinypic.com/ofivo.jpg

和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 --> 

登錄不工作對我來說

login that doesn't work for me http://i49.tinypic.com/13zcqj6.jpg

,它的HTML

<form class="login" method="post"> <fieldset> 
<legend>Members Login</legend> 

<div> 
<label for="auth_username">Username</label> <input id="auth_username" name="auth_username"> 
</div> 

<div> 
<label for="auth_password">Password</label> <input id="auth_password" name="auth_password" type="password"> 
</div> 

</fieldset> 
<div class="buttons"> 
<input name="auth_login" type="submit" value="Login"><p class="note"><a href="/forgotpassword">Forgot your password?</a></p> 

</div> 

</form> 

我的腳本幾乎是在這兩種情況下是相同的。

require 'rubygems' 
require 'mechanize' 
#agent = WWW::Mechanize.new 

agent = WWW::Mechanize.new 

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

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


puts 
login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }  
login_form['vb_login_username'] = 'user name' 
login_form['vb_login_password'] = '' 

page = agent.submit login_form 

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

機械化日誌登錄不起作用

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 
+0

@Radek,這是一個網絡流?你做了什麼和做不到的做了什麼不同? – 2010-01-30 06:03:40

+0

@Wayne康拉德:我更新了我的問題。我沒有做任何不同的事情,只是在我的腳本不起作用的情況下使用密碼字段。在另一個我使用密碼的MD5散列。我覺得麻煩的是,通信網絡服務器<=>瀏覽器不止一行,機械化不處理它。 – Radek 2010-01-30 06:36:56

回答

0

是,捕捉餅乾(例如,通過FireCookies插件在Firefox),並手動將其傳遞到機械化可以爲大多數情況下工作。

+0

@ S.Mark:問題是我必須通過什麼以及如何:-)看到上面的圖片。 – Radek 2010-01-30 05:54:11

+0

嗨,Firebug的FireCookie擴展,https://addons.mozilla.org/ja/firefox/addon/6683,這裏是關於cookie選項卡的屏幕截圖https://addons.mozilla.org/en-US/ firefox/images/p/18997/1204922922,你需要將這些cookie傳遞給機械化 – YOU 2010-01-30 06:24:14

+0

@ S.Mark:謝謝你。現在我正在考慮下一步是什麼。我從機械化調用新的網址?但是哪一個?或者我在我的Web瀏覽器中成功登錄後保存頁面,然後將我的ruby腳本加載到'page object'中? – Radek 2010-01-30 06:48:58

1

您的問題很可能來自Mechanize只跟蹤使用Set-Cookie HTTP標頭創建的Cookie。它無法處理由JavaScript創建的Cookie。

+0

這裏有任何解決方案。 – Sharad 2011-09-08 17:25:07