2011-02-18 46 views
4

我正在嘗試使用機械化提交表單。但在提交時沒有任何反應。我只是再次登錄頁面。無法使用表單登錄使用機械化

形式:

http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php

require 'Mechanize' 
agent = WWW::Mechanize.new 
agent.get("http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php") 

form = agent.page.forms.first 
form.username = 'username' 
form.password = 'password' 
form.submit 

直播HTTP日誌時登錄與瀏覽器:

http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php 

POST /partnersystem/mylogins.php HTTP/1.1 
Host: affilate.mikkelsenmedia.dk 
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; da; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: da,en-us;q=0.7,en;q=0.3 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 115 
Connection: keep-alive 
Referer: http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php 
Cookie: XXX 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 47 
username=username&password=password&send=Submit 
HTTP/1.1 200 OK 
Date: Fri, 18 Feb 2011 17:07:15 GMT 
Server: Apache/2.0.63 (CentOS) 
X-Powered-By: PHP/5.1.6 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Content-Length: 77 
Connection: close 
Content-Type: text/html; charset=UTF-8 

控制檯:

irb(main):001:0> require 'Mechanize' 
=> true 
irb(main):002:0> agent = Mechanize.new { |agent| 
irb(main):003:1* agent.user_agent_alias = 'Mac Safari' 
<"http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php") 
=> #<Mechanize::Page 
{url 
    #<URI::HTTP:0x2a1c770 URL:http://affilate.mikkelsenmedia.dk/partnersystem/mylo 
gins.php>} 
{meta} 
{title "Partner System - Log-in"} 
{iframes} 
{frames} 
{links} 
{forms 
    #<Mechanize::Form 
    {name "loginform"} 
    {method "POST"} 
    {action "mylogins.php"} 
    {fields 
    #<Mechanize::Form::Text:0x2836680 
    @name="username", 
    @node= 
     #(Element:0x141b3e8 { 
     name = "input", 
     attributes = [ 
      #(Attr:0x13e4458 { name = "type", value = "text" }), 
      #(Attr:0x13e4440 { name = "name", value = "username" }), 
      #(Attr:0x13e43e0 { name = "size", value = "30" })] 
     }), 
    @value=""> 
    #<Mechanize::Form::Field:0x2836230 
    @name="password", 
    @node= 
     #(Element:0x141b22c { 
     name = "input", 
     attributes = [ 
      #(Attr:0x13ac148 { name = "type", value = "password" }), 
      #(Attr:0x13ac13c { name = "name", value = "password" }), 
      #(Attr:0x13ac10c { name = "size", value = "30" })] 
     }), 
    @value="">} 
    {radiobuttons} 
    {checkboxes} 
    {file_uploads} 
    {buttons 
    #<Mechanize::Form::Submit:0x2835f90 
    @name="send", 
    @node= 
     #(Element:0x141b01c { 
     name = "input", 
     attributes = [ 
      #(Attr:0x13702e0 { name = "type", value = "submit" }), 
      #(Attr:0x13702d4 { name = "name", value = "send" }), 
      #(Attr:0x13702c8 { name = "class", value = "style2" }), 
      #(Attr:0x13702bc { name = "value", value = "Submit" })] 
     }), 
    @value="Submit">}>}> 

irb(main):006:0> form = agent.page.forms.first 
irb(main):007:0> form.username = 'username' 
=> "username" 
irb(main):008:0> form.password = 'password' 
=> "password" 
irb(main):009:0> form.submit 
=> #<Mechanize::Page 
{url 
    #<URI::HTTP:0x2a82e78 URL:http://affilate.mikkelsenmedia.dk/partnersystem/mylo 
gins.php>} 
{meta} 
{title "Partner System - Log-in"} 
{iframes} 
{frames} 
{links} 
{forms 
    #<Mechanize::Form 
    {name "loginform"} 
    {method "POST"} 
    {action "mylogins.php"} 
    {fields 
    #<Mechanize::Form::Text:0x2a52c50 
    @name="username", 
    @node= 
     #(Element:0x1529694 { 
     name = "input", 
     attributes = [ 
      #(Attr:0x1513c14 { name = "type", value = "text" }), 
      #(Attr:0x1513c08 { name = "name", value = "username" }), 
      #(Attr:0x1513bfc { name = "size", value = "30" })] 
     }), 
    @value=""> 
    #<Mechanize::Form::Field:0x2a52998 
    @name="password", 
    @node= 
     #(Element:0x1529550 { 
     name = "input", 
     attributes = [ 
      #(Attr:0x15121d4 { name = "type", value = "password" }), 
      #(Attr:0x15121c8 { name = "name", value = "password" }), 
      #(Attr:0x15121bc { name = "size", value = "30" })] 
     }), 
    @value="">} 
    {radiobuttons} 
    {checkboxes} 
    {file_uploads} 
    {buttons 
    #<Mechanize::Form::Submit:0x2a52758 
    @name="send", 
    @node= 
     #(Element:0x152940c { 
     name = "input", 
     attributes = [ 
      #(Attr:0x151062c { name = "type", value = "submit" }), 
      #(Attr:0x1510614 { name = "name", value = "send" }), 
      #(Attr:0x1510608 { name = "class", value = "style2" }), 
      #(Attr:0x15105fc { name = "value", value = "Submit" })] 
     }), 
    @value="Submit">}>}> 

irb(main):010:0> 
+0

@Railsbeginner請修正代碼中使用非棄用常數和更新後IRB步驟都沒有噪音。 – Phrogz 2011-02-20 22:06:23

+0

@Progrog現在好了嗎? – 2011-02-20 22:24:23

+0

當您執行失敗的登錄時,它將閃爍一個頁面,其中包含一個元刷新。你可能想玩弄。 agent.follow_meta_refresh選項 – 2011-03-02 04:46:54

回答

11

服務器還檢查提交按鈕參數「發送」的存在。

form.submit

form.add_field! 'send','Submit' 
2

有一件事我看到運行你Ë xample:

>> agent = WWW::Mechanize.new 
!!!!! DEPRECATION NOTICE !!!!! 
The WWW constant is deprecated, please switch to the new top-level Mechanize 
constant. WWW will be removed in Mechanize version 2.0 

You've referenced the WWW constant from (irb):3:in `irb_binding', please 
switch the "WWW" to "Mechanize". Thanks! 

Sincerely, 

    Pew Pew Pew 

我沒有該系統上的帳戶,因此登錄對我無效。如果您在IRB中執行初始步驟,您會看到什麼?

0

加入這一行雖然運行「代理= WWW :: Mechanize.new」你會得到棄用警告爲「WWW」不斷在最新版本過時。你需要運行沒有'WWW'常量的命令。更改命令是:

劑= Mechanize.new

感謝