Comment optimisé ce script
Bonjour j'ai un web crawler que je cherche a optimisé pour me rapprocher le plus possible de ces capacités de base
voici le crawler tel qu'il est a la base pas beau mais super rapide et super stable
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #!/usr/bin/perl -w
use strict;
use warnings;
my $VERSION = "0.8";
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
$| = 1;
sub spider (%);
spider URL => '$url';
sub spider (%) {
my %args = @_;
my @startlinks = ("http://clubic.com");
push(@startlinks, $args{URL});
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible)');
WORKLOOP: while (my $link = shift @startlinks) {
for (my $i = 0; $i< $#startlinks; $i++) {
next WORKLOOP if $link eq $startlinks[$i];
}
print ">>>>> working on $link\n";
HTML::LinkExtor->new(
sub {
my ($t, %a) = @_;
my @links = map { url($_, $link)->abs() }
grep { defined } @a{qw/href img/};
foreach my $start_link (@startlinks) {
my $i = 0;
for (0 .. $#links) {
if ($links[$i++] eq $start_link) {
$links[$i -1] = "'REMOVE'";
}
}
}
@links = sort @links;
for (my $i = 0; $i< $#links; $i++) {
$links[$i] = "'REMOVE'" if $links[$i] eq $links[$i +1];
}
@links = grep { $_ ne "'REMOVE'" } @links;
print "+ $_\n" foreach @links;
push @startlinks, @links if @links;
} ) -> parse(
do {
my $r = $ua->simple_request
(HTTP::Request->new("GET", $link));
$r->content_type eq "text/html" ? $r->content : "";
}
)
}
} |
J'ai ajouté quelque fonctions a ce robot ce qui la fait devenire un monstre stable mais moins rapide et presque illisible 8O
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| #!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
use HTTP::Request;
use HTTP::Response;
use HTTP::Size;
use POSIX;
my $VERSION = "0.8";
$| = 1;
#**************************************
# Ont entre sur le net par la
#**************************************
my @startlinks = (
"http://clubic.com"
);
#**************************************
# Configuration
#**************************************
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible)');
$ua->protocols_allowed( ['http'] );
$ua->timeout(4);
$ua->requests_redirectable( ['HEAD'] );
$ua->max_redirect(5);
$ua->protocols_forbidden( [ 'mailto', 'https', 'gopher', 'ftp', 'socks', 'file' ] );
#**************************************
# Le robot
#**************************************
sub spider (%);
spider URL => '$url';
sub spider (%) {
my %args = @_;
push( @startlinks, $args{URL} );
WORKLOOP: while ( my $link = shift @startlinks ) {
for ( my $i = 0 ; $i < $#startlinks ; $i++ ) {
next WORKLOOP if $link eq $startlinks[$i];
}
HTML::LinkExtor->new(
sub {
my ( $t, %a ) = @_;
my @links = map { url( $_, $link )->abs() } grep { defined } @a{qw/href img/};
foreach my $start_link (@startlinks) {
my $i = 0;
for ( 0 .. $#links ) {
if ( $links[ $i++ ] eq $start_link ) {
$links[ $i - 1 ] = "'REMOVE'";
}
}
}
@links = sort @links;
for ( my $i = 0 ; $i < $#links ; $i++ ) {
$links[$i] = "'REMOVE'" if $links[$i] eq $links[ $i + 1 ];
}
@links = grep { $_ ne "'REMOVE'" } @links;
# Ici commence mon code
@links = allow(@links);
my $nouveau_liens = join( "", @links );
my $request = HTTP::Request->new( 'GET', $nouveau_liens );
my $res = $ua->request($request);
if ( $res->is_success )
{
my $code = $res->content;
my $date = date();
my $taille = taille($nouveau_liens);
my %html = (
resultat => [
"$nouveau_liens", "$code",
"$date", "$taille ko"
]
);
print "$html{'resultat'}->[0]\n";
print "$html{'resultat'}->[1]\n";
print "$html{'resultat'}->[2]\n";
print "$html{'resultat'}->[3]\n";
}
}
# Il fini la
push @startlinks, @links if @links;
}
)->parse(
do {
my $r = $ua->simple_request( HTTP::Request->new( "GET", $link ) );
$r->content_type eq "text/html" ? $r->content : "";
}
);
}
}
#**************************************
# Suppréssion des extensions interdites
#**************************************
use Regexp::Assemble;
{
my $re;
sub allow {
if ( not $re ) {
my $r = Regexp::Assemble->new;
$r->add("\\.$_\$") for
qw/
htm asp aspx nux mspx cfm html xhtml jhtml php php3
php4 shtml jsp php4 php5 pm shtm jpg jpeg png gif
xls rtf doc pdf
/;
$re = $r->re;
}
return grep { /$re/ } @_;
}
}
#**********************************
# Ont cherche la date
#**********************************
sub date
{
my (@date);
my ( $an, $jour, $mois ) = ( '', '', '' );
@date = gmtime( time() );
if ( length( $date[5] ) == 2 )
{
if ( $date[5] > 70 ) { $an = '19'; }
else { $an = '20'; }
}
if ( length( $date[5] ) == 3 ) { $date[5] = $date[5] + 1900; }
if ( length( $date[5] ) == 4 ) { }
$an .= $date[5];
$jour = $date[3];
if ( length($jour) == 1 ) { $jour = '0' . $jour }
$mois = $date[4];
$mois++;
if ( length($mois) == 1 ) { $mois = '0' . $mois }
return ("$jour/$mois/$an");
}
#**********************************
# Calcul de la taille
#**********************************
sub taille
{
my $url = join("", @_);
my $size = HTTP::Size::get_size( $url );
if (defined $size)
{
my $resultat = $size/1024;
$resultat = floor($resultat);
return $resultat;
} else {
return "inconnue";
}
} |
En fait j'aimerais pourvoir le séparer en plusieur sous programme pour pouvoir y ajouté les threads threads::queue avec votre aide , et aussi le support des fihciers robots.txt
Je suis ouvert a toutes suggestion :D (ne me parler pas de www::robot mon robot est a but expérimentale heu enfin quand il seras fini)
Merci d'avance :)