2014-10-17 30 views
0

我正在嘗試將automaticaly連接到使用機械化的網站。爲什麼使用網站上的Mechanize登錄重定向到主頁?

我看過搜索到的互聯網,但找不到任何解決方案給我的問題,這就是爲什麼在運行form.submit後,我回到了主頁?

當我嘗試連接時是否可以獲取日誌?

我的代碼:

mechanize = Mechanize.new do |agent| 
    agent.user_agent_alias = "Linux Mozilla" 
end 

page = mechanize.get("http://website.org/") 
form = mechanize.page.forms.first 
    puts form.content 
form.username = "mylogin" 
form.password = "mypassword" 
form.submit 

該網站的代碼片段:

<div id="signincontainer"> 
    <form method="POST" action="http://website.org/" name="FL" id="signin"> 
     <input name="op" value="login" type="hidden"> 
     <input name="redirect" value="" type="hidden"> 
     <span class="signinq"> 
     <input style="background: url('images/username.png') no-repeat scroll 5px 50% rgb(255, 255, 255);" id="username" name="login" title="username" tabindex="4" type="text"> 
     <a class="donthaveaccount" href="http://website.org/signup.html"><span> 
     Sign Up 
     </span></a> </span> <span class="signinq"> 
     <input style="background: url('images/password.png') no-repeat scroll 5px 50% rgb(255, 255, 255);" id="password" name="password" value="" title="password" tabindex="5" type="password"> 
     <a class="forgotpassword" href="http://website.org/forgot-pass.html" id="resend_password_link"><span> 
     Forgot your password? 
     </span></a> </span> 
     <input id="signin_submit" value="Enviar" tabindex="6" src="images2/signin.png" type="image"> 
    </form> 
    </div> 

我運行的Ruby 1.9.3-P484和機械化2.0.1。

+0

有可能是任何數量的爲什麼你會被重定向的原因。你正試圖設置一個'user_agent_alias',但是如果你想[模仿Mozilla]那個簽名不太現實(https://developer.mozilla.org/en-US/docs/Web/HTTP/ Gecko_user_agent_string_reference)。 – 2014-10-17 15:51:52

+0

我嘗試沒有user_agent_alias,我得到相同的錯誤。我無法連接 – user2912390 2014-10-17 16:28:45

+0

如果您使用的是Mozilla爲Windows或Linux瀏覽器發送的內容的完全副本,會發生什麼情況?不能保證問題存在,但是如果字符串匹配不夠好,某些站點會拒絕連接。他們不應該但他們這樣做。 – 2014-10-17 17:47:22

回答

0

錫人我發現Mozilla的完整ID,但我得到同樣的問題,當我沒有在密碼中寫任何東西,我得到了錯誤的密碼或登錄,但是當我寫我的密碼,我什麼也得不到?

我改進了我的代碼。

agent = Mechanize.new {| a | a.log = Logger.new( 「Mechanize.log」)} agent.read_timeout = 60

def add_cookie(agent, uri, cookie) 
    uri = URI.parse(uri) 
    Mechanize::Cookie.parse(uri, cookie) do |cookie| 
    agent.cookie_jar.add(uri, cookie) 
    end 
end 

agent.user_agent_alias = 'Linux Mozilla' 
     { 
      'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624', 
      'Linux Firefox' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1' 
     } 

    agent.get("http://youwatch.org/?op=my_files") 
      form = agent.page.parser.css('form')[0] 
      agent.page.forms[0]["login"] = "zyriuse" 
      agent.page.forms[0]["password"] = "[email protected]" 
     f = agent.page.forms[0].submit 
    puts f.content 
+0

上面的代碼工作.i不知道爲什麼我每次運行這個腳本時得到一個錯誤的cookie – user2912390 2014-10-22 15:30:51

相關問題