如何在使用WWW :: Mechanize提交表单后获取页面内容?

时间:2014-04-02 09:05:45

标签: perl

我有一个由登录表单组成的网页。当我在perl中使用WWW :: Mechanize模块提交表单时,我的页面内容会发生变化,因为用户名/密码错误。我想在提交表单后抓住页面的内容。我试着用这个:

$mech->content;

但那不是我需要的。

这是我的一段代码:

$fields = {
    'usr' => $username,
    'passwd' => $password[0]  
};
$url = "http://blahblah.com/login/index.aspx?id=1";
$mech = WWW::Mechanize->new();
$mech->get($url);
$mech->submit_form(
    form_number=> 1,
    fields => $fields,
    button => 'button'
)
$mech->content;

另见:How to receive content of a page after submitting a form in perl?

1 个答案:

答案 0 :(得分:0)

以下代码适用于我

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/ say /;
use WWW::Mechanize;

my $username = '';
my $password = '';

my $fields = {
    usr    => $username,
    passwd => $password,
};

my $url  = "http://localhost:3000/login";
my $mech = WWW::Mechanize->new();

$mech->get($url);
$mech->submit_form(
    form_number => 1,
    fields      => $fields,
    button      => 'button'
);

say $mech->content;