2011-12-19 80 views
0

我想用perl登錄(穩定)到像http://site/signin/index.php這樣的網站。 此頁面有表格並提交沒有名字。 我如何登錄? 這是一個示例代碼登錄:用www :: mechanize登錄未命名的表單,用未命名的提交

my $mech = WWW::Mechanize->new(); 
$mech -> cookie_jar(HTTP::Cookies->new()); 
$mech -> get($login_url); 
$mech -> form_name('theform'); 
$mech -> field ('username' => $username); 
$mech -> field ('password' => $password); 
$mech -> click ('log in'); 
print $mech-> content(); 

這是我的形式

<form method="post" action=""> 
<table id="regform" cellspacing="5"> 
<tr> 
<td class="regparam">email:</td> 
<td><input type="text" name="email" value="" tabindex="1" maxlength="100" style="font-family: Tahoma; font-size: 10pt; font-weight: bold; border: 1px solid #9AD7F8; background-color: #ffffff" class="reginp" /></td> 
</tr> 
<tr> 
<td class="regparam">password:</td> 
<td><input type="password" name="password" value="" tabindex="2" maxlength="100" style="font-family: Tahoma; font-size: 10pt; font-weight: bold; border: 1px solid #9AD7F8; background-color: #ffffff" class="keyboardInput" /></td> 
</tr> 
<tr> 
<td></td> 
<td valign="middle"> 
<button type="submit" style="font-weight:bold; tabindex="4" class="subm-posts">login</button> 
<label style="margin:0px; padding:0px; margin-left:10px; margin-top:7px; float:left; clear:none;"> 
<input type="checkbox" name="rememberme" value="1" tabindex="3" /> 
<span style="padding:2px; padding-left:5px;">remember me</span> 
</label> 
</td> 
</tr> 
</table> 
</form> 
+0

顯示相關的HTML。 – daxim 2011-12-19 04:13:56

回答

1

而不是用名字來指定的形式,你可以參考它由number,即它出現在表格中:

$mech->form_number(1); 

表格編號從一開始。

同樣,因爲只有one button的形式,你可以只使用

$mech->click(); 

,這將提交表單,因爲它會按一下按鈕。

+0

真的很感謝馬丁 – thi 2011-12-19 15:14:53

0
my $mech = WWW::Mechanize->new(); 
$mech->cookie_jar(HTTP::Cookies->new()); 
$mech->get($login_url); 

$mech->submit_form(
    form_name => 'theform', 
    fields => { 
     username => $username, 
     password => $password, 
    }, 
); 

print $mech->content(); 
+0

請閱讀標題併發送答案! – thi 2011-12-19 14:03:27

+0

@ sali444請發佈正確的代碼以獲得正確的答案,稍後編輯將不會更改以前發佈的答案。 – Pradeep 2011-12-20 09:44:27