Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
WWW:::Mechanize YAPC::BR 2008
1. WWW::Mechanize
Márcio Vitor De Matos Silva cromo.jml@gmail.com
2. 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
3. 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
4. De onde vem a força ?
LWP::UserAgent
HTTP::Request
HTTP::Cookies
HTML::TokeParser
URI
...
5. LWP::UserAgent
WWW::Mechanize
Conceitos e propósitos diferentes.
Mechanize abstrai dificuldades do
LWP::UserAgent
6. Ambiente
Instalação
cpan> install WWW::Mechanize
Documentação
perldoc WWW::Mechanize
Uso
use WWW::Mechanize;
Construtor
my $mech = WWW::Mechanize>new();
7. Capturando uma página
use WWW::Mechanize;
my $mech = WWW::Mechanize>new();
$mech>get(“http://www.retbeer.com/”);
print $mech>title.$/;
16. Realizando testes
use 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' ,quot;Basequot;);
$mech>title_is( quot;Perl Brasil WebHomequot;,quot;Titlequot;);
$mech>content_contains( quot;Artigos quot;,quot;Artigosquot; );
# perl teste_perlbr.pl
1..4
ok 1 GET http://www.perl.org.br/
ok 2 Base
ok 3 Title
ok 4 Artigos
17. Exemplo de uso real
my $login = quot;login_namequot;;
my $password = quot;passwordquot;;
my $folder = quot;folderquot;;
my $url = quot;http://img78.photobucket.com/albums/v281/$login/$folder/quot;;
# 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 quot;$_nquot;;
$mech>form_number(2);
$mech>field('the_file[]' => $_);
$mech>submit();
}
18. Referências
CPAN
http://search.cpan.org/dist/WWW
Mechanize/lib/WWW/Mechanize.pm
Mailing list
http://groups.google.com/group/wwwmechanize
users
Google Code
http://code.google.com/p/wwwmechanize/
19. 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