Módulos de Web no Perl
Neste artigo apresento alguns métodos para trabalhar com "tags" e "reg" em Perl de maneira simples e prática usando alguns módulos.
Parte 2: Usando LinkExtor
O programa a seguir mostra o uso de outro módulo bacana de Web em Perl, o HTML::LinkExtor, que extrai os links de uma página exemplo.
Damos uma página para ele extrair com os dados:
Veja sua saída:
Type: link
href -> style.css
Type: body
background -> back.gif
Type: a
href -> http://www.perl.com/
Type: img
src -> test.gif
Type: a
href -> http://www.pm.org
Type: img
src -> pm.gif
lowsrc -> pmsmall.gif
Ele extraiu os links e mostrou o tipo de link "Type". Se você quiser apenas os links do "Type: a", o script fica da seguinte forma:
use HTML::LinkExtor;
my $file = shift;
my $p = HTML::LinkExtor->new;
$p->parse_file($file);
my @links = $p->links;
foreach (@links) {
print 'Type: ', shift @$_, " ";
while (my ($name, $val) = splice(@$_, 0, 2)) {
print " $name -> $val ";
}
}
my $file = shift;
my $p = HTML::LinkExtor->new;
$p->parse_file($file);
my @links = $p->links;
foreach (@links) {
print 'Type: ', shift @$_, " ";
while (my ($name, $val) = splice(@$_, 0, 2)) {
print " $name -> $val ";
}
}
Damos uma página para ele extrair com os dados:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head><title>Test HTML Page</title>
<link rel=stylesheet type='text/css' href='style.css'></head>
<body background="back.gif">
<h1 ALIGN=center>test HTML Page</h1>
<p>This is the first paragraph.
It contains a <a href="http://www.perl.com/">link</a></p>
<p><font color="#0000FF">This</font> is the 2nd paragraph.
It contains an image - <img src="test.gif"></p>
<p>Here is an image used as a link<br>
<a href="http://www.pm.org"><img src="pm.gif"
lowsrc="pmsmall.gif"></a></p>
</body>
</html>
<html>
<head><title>Test HTML Page</title>
<link rel=stylesheet type='text/css' href='style.css'></head>
<body background="back.gif">
<h1 ALIGN=center>test HTML Page</h1>
<p>This is the first paragraph.
It contains a <a href="http://www.perl.com/">link</a></p>
<p><font color="#0000FF">This</font> is the 2nd paragraph.
It contains an image - <img src="test.gif"></p>
<p>Here is an image used as a link<br>
<a href="http://www.pm.org"><img src="pm.gif"
lowsrc="pmsmall.gif"></a></p>
</body>
</html>
Veja sua saída:
Type: link
href -> style.css
Type: body
background -> back.gif
Type: a
href -> http://www.perl.com/
Type: img
src -> test.gif
Type: a
href -> http://www.pm.org
Type: img
src -> pm.gif
lowsrc -> pmsmall.gif
Ele extraiu os links e mostrou o tipo de link "Type". Se você quiser apenas os links do "Type: a", o script fica da seguinte forma:
use HTML::LinkExtor;
my $file = shift;
my $p = HTML::LinkExtor->new(&check);
$p->parse_file($file);
my @links;
foreach (@links) {
print 'Type: ', shift @$_, " ";
while (my ($name, $val) = splice(@$_, 0, 2)) {
print " $name -> $val ";
}
}
sub check {
push @links, [@_] if $_[0] eq 'a';
}
my $file = shift;
my $p = HTML::LinkExtor->new(&check);
$p->parse_file($file);
my @links;
foreach (@links) {
print 'Type: ', shift @$_, " ";
while (my ($name, $val) = splice(@$_, 0, 2)) {
print " $name -> $val ";
}
}
sub check {
push @links, [@_] if $_[0] eq 'a';
}
Tony posso te fazer umas perguntas,
o OpenBSD já está usando o driver nvidia ??????
E desktop 3d, algum dia vai ter ????????????
vlw ....