WWW::MechanizeMárcio Vitor De Matos Silva [email protected]
WWW::Mechanize
Criador e Project Owner Andy Lester 'PetDance'
Licença Artistic License/GPL
Versão estável 1.34 – 09/12/2007
Versão desenvolvimento 1.49_01 – 27/09/2008
Usos úteis e comuns Preenchimento de formulários
Percorrer e clicar links
Testes automatizados
Enviar referers, useragents, uso de proxys
Acessar histórico de urls visitados
De onde vem a força ?
LWP::UserAgent HTTP::Request HTTP::Cookies HTML::TokeParser URI ...
LWP::UserAgentWWW::Mechanize
Conceitos e propósitos diferentes.
Mechanize abstrai dificuldades do LWP::UserAgent
AmbienteInstalação
cpan> install WWW::MechanizeDocumentação
perldoc WWW::MechanizeUso
use WWW::Mechanize;Construtor
my $mech = WWW::Mechanize>new();
Capturando uma página
use WWW::Mechanize;my $mech = WWW::Mechanize>new();$mech>get(“http://www.retbeer.com/”);print $mech>title.$/;
$mech>agent
Get/set que define o useragent usado durante a navegação
Também pode ser definida no método construtor:
my $mech = WWW::Mechanize>new( agent => ’wonderbot 1.01’ );
Enviando um agentmy $mech = new WWW::Mechanize;$mech>get('http://www.perl.org.br');$mech>agent(“TestBot/1.0”);if($mech>success) {
print $mech>content;}else {
print $mech>response>error_as_HTML;}
$mech>add_header Permite adicionar parâmetros ao header da
requisição
$mech>add_header( Referer => 'http://www.retbeer.com' );
$mech>add_header( Referer => undef );
$mech>add_header( Encoding => ’text/klingon’ );
$mech>find_all_links
Retorna todos os links seguindo algum critério: $mech>get( $url ); my @links = $mech>find_all_links( text_regex => qr/Cinema/i ); foreach my $link (@links) { $mech>get( $link>url_abs ); return $mech>content;
}
$mech>follow_link 'Clica' o link seguinte o critério escolhido
$mech>get('http://www.retbeer.com');foreach my $categoria qw(vinho cerveja vodka) {
$mech>follow_link( text_regex => qr/$categoria/i );if ($mech>success) {
print $categoria.$/;print $mech>content;
$mech>back;}
}
$mech>submit_form
Formulários por form_number/form_name
Preenchimento de campos
Submit de forms
Submetendo um formulário use WWW::Mechanize; my $mech = WWW::Mechanize>new(); $mech>get( 'http://www.retbeer.com' );
$mech>submit_form( form_name => 'form_login', fields => { username => 'mungo', password => 'lostandalone', } );
Test::WWW::Mechanize
Sub classe WWW::Mechanize Incorpora features para testes de aplicações
web: Base Conteúdo html Links Formulários
Realizando testesuse Test::WWW::Mechanize;use Test::More tests => 4;
my $mech = Test::WWW::Mechanize>new;$mech>get_ok( 'http://www.perl.org.br/ ');$mech>base_is( 'http://www.perl.org.br/Main/WebHome' ,"Base");$mech>title_is( "Perl Brasil WebHome","Title");$mech>content_contains( "Artigos ","Artigos" );
# perl teste_perlbr.pl1..4ok 1 GET http://www.perl.org.br/ ok 2 Baseok 3 Titleok 4 Artigos
Exemplo de uso real my $login = "login_name"; my $password = "password"; my $folder = "folder"; my $url = "http://img78.photobucket.com/albums/v281/$login/$folder/"; # login to your photobucket.com account my $mech = WWW::Mechanize>new(); $mech>get($url); $mech>submit_form( form_number => 1, fields => { password => $password }, ); die unless ($mech>success); # upload image files specified on command line foreach (@ARGV) { print "$_\n"; $mech>form_number(2); $mech>field('the_file[]' => $_); $mech>submit(); }
Referências
CPANhttp://search.cpan.org/dist/WWW
Mechanize/lib/WWW/Mechanize.pm Mailing list
http://groups.google.com/group/wwwmechanizeusers
Google Code http://code.google.com/p/wwwmechanize/
Fontes de pesquisa: man WWW::Mechanize Spidering Hacks
HEMENWAY, Kevin e CALISHAIN, Tara. Cambridge, Massachusetts: O'Reilly, 2003.ISBN 0596005776.
Automated web site testing with WWW::Mechanize with Andy Lester
http://www.webgui.org/wgtv/wwwmechanize
THE END