20
    WWW::Mechanize Márcio Vitor De Matos Silva - [email protected]

WWW:::Mechanize YAPC::BR 2008

  • Upload
    mvitor

  • View
    2.875

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: WWW:::Mechanize YAPC::BR 2008

   

   WWW::MechanizeMárcio Vitor De Matos Silva ­ [email protected]

Page 2: WWW:::Mechanize YAPC::BR 2008

   

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 

Page 3: WWW:::Mechanize YAPC::BR 2008

   

Usos úteis e comuns  Preenchimento de formulários

 Percorrer e clicar links

 Testes automatizados

 Enviar referers, user­agents, uso de  proxys

 Acessar histórico de urls visitados

Page 4: WWW:::Mechanize YAPC::BR 2008

   

De onde vem a força ?

 LWP::UserAgent HTTP::Request HTTP::Cookies HTML::TokeParser URI ...

Page 5: WWW:::Mechanize YAPC::BR 2008

   

LWP::UserAgentWWW::Mechanize

 Conceitos e propósitos diferentes.

 Mechanize abstrai dificuldades do LWP::UserAgent

Page 6: WWW:::Mechanize YAPC::BR 2008

   

AmbienteInstalação

cpan> install WWW::MechanizeDocumentação

perldoc WWW::MechanizeUso

use WWW::Mechanize;Construtor

my $mech = WWW::Mechanize­>new();

Page 7: WWW:::Mechanize YAPC::BR 2008

   

Capturando uma página

use WWW::Mechanize;my $mech = WWW::Mechanize­>new();$mech­>get(“http://www.retbeer.com/”);print $mech­>title.$/;

Page 8: WWW:::Mechanize YAPC::BR 2008

   

$mech­>agent

Get/set que define o user­agent usado durante a navegação

Também pode ser definida no método construtor:

my $mech = WWW::Mechanize­>new( agent => ’wonderbot 1.01’ );

Page 9: WWW:::Mechanize YAPC::BR 2008

   

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;}

Page 10: WWW:::Mechanize YAPC::BR 2008

   

$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’ );

Page 11: WWW:::Mechanize YAPC::BR 2008

   

$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;

}

Page 12: WWW:::Mechanize YAPC::BR 2008

   

$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;}

}

Page 13: WWW:::Mechanize YAPC::BR 2008

   

$mech­>submit_form

 Formulários por form_number/form_name

 Preenchimento de campos

 Submit de forms

       

Page 14: WWW:::Mechanize YAPC::BR 2008

   

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    => 'lost­and­alone',                }           ); 

Page 15: WWW:::Mechanize YAPC::BR 2008

   

Test::WWW::Mechanize

  Sub classe WWW::Mechanize Incorpora features para testes de aplicações 

web: Base Conteúdo html Links Formulários

Page 16: WWW:::Mechanize YAPC::BR 2008

   

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

   

Page 17: WWW:::Mechanize YAPC::BR 2008

   

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();    }

Page 18: WWW:::Mechanize YAPC::BR 2008

   

Referências

CPANhttp://search.cpan.org/dist/WWW­

Mechanize/lib/WWW/Mechanize.pm Mailing list 

http://groups.google.com/group/www­mechanize­users

Google Code http://code.google.com/p/www­mechanize/

Page 19: WWW:::Mechanize YAPC::BR 2008

   

Fontes de pesquisa: man WWW::Mechanize Spidering Hacks

HEMENWAY, Kevin e CALISHAIN, Tara. Cambridge, Massachusetts: O'Reilly, 2003.ISBN 0­596­00577­6.

 Automated web site testing with WWW::Mechanize  with Andy Lester

http://www.webgui.org/wgtv/wwwmechanize

Page 20: WWW:::Mechanize YAPC::BR 2008

   

THE END