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
| #!/usr/bin/perl -w
use strict;
use URI::URL;
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;
require LWP::Parallel::UserAgent;
use POSIX;
use HTTP::Size;
use HTML::Parse qw(parse_html);
#===========================================================================#
# Begin of configuration #
#===========================================================================#
my $arg;
$| = 1;
my $VERSION = "Bot/1.1";
my $ua = LWP::Parallel::UserAgent->new('Mozilla/5.0 (compatible; Bot/1.1');
$ua->max_hosts(400);
$ua->max_req(400);
$ua->wait(0);
$ua->max_redirect(8000000000);
$ua->protocols_allowed( [ 'http' ] );
$ua->protocols_forbidden( [ 'file', 'mailto', 'https', 'ftp', 'socks', 'gopher'] );
$ua->requests_redirectable( [ 'HEAD' ] );
$ua->timeout(2);
$ua->env_proxy;
$ua->remember_failures(1);
$ua->redirect(1);
my $cookie_jar = HTTP::Cookies->new(
file => 'lwp_cookies.dat',
autosave => 1,
);
$ua->cookie_jar($cookie_jar);
#===========================================================================#
# The Robot #
#===========================================================================#
my %urls;
@urls{ @ARGV } = (1) x @ARGV;
robot (keys %urls);
# Ecrire un log d'evenement
BEGIN {
use CGI::Carp qw(carpout);
open(LOG, ">>/log_spider.txt") or die "Impossible d'ouvrir : $!\n";
carpout(*LOG);
}
sub robot
{
foreach my $link ( @_ ) {
my $confirm = verif_link_format($link);
if ( $confirm == 1) {
my $req = HTTP::Request->new( 'GET', $link );
$req->header('Accept' => [
qw(
text/html application/xhtml+xml application/msexcel
application/msword application/pdf application/rtf
application/x-javascript application/x-httpd-php
text/rtf text/nux application/sxc application/sxw
application/sxd application/mdi application/x-httpd-asp
application/x-httpd-aspx application/x-httpd-mspx
application/x-httpd-cfm application/x-httpd-jthml
application/x-httpd-pm )
]
);
my $res = $ua->request($req, $arg, 4096);
if ( $res->is_success ) {
my ($code, $date, $size, %page, $base, $content_type);
$code = $res->content;
$date = date();
$size = size($link);
$base = $res->base;
$content_type = $res->content_type;
%page = (
result => [
"$code", "$link",
"$date", "$size KB",
"$content_type"
]
);
#print "$page{'result'}->[0]\n";
print "=====================\n";
print "$page{'result'}->[1]\n";
print "$page{'result'}->[2]\n";
print "$page{'result'}->[3]\n";
print "$page{'result'}->[4]\n";
print "====================\n";
extor_links($code,$base);
}
}
}
}
#===========================================================================#
# Extract links #
#===========================================================================#
sub extor_links
{
my($url, %saw, @url);
my $html=$_[0];
my $base=$_[1];
foreach ($html) {
my $ht_tree = parse_html($_[0]);
my ($linkpair, $fqurl);
foreach $linkpair (@{$ht_tree->extract_links(qw<a>)}) {
my($link,$elem) = @$linkpair;
push(@url, $fqurl)
unless $saw{ $fqurl = url($link,$base)->abs->as_string }++;
}
my %keys_url = sort keys %saw;
robot(%keys_url);
}
}
#===========================================================================#
# Keep only good extension to extor #
#===========================================================================#
sub verif_link_format
{
foreach my $link_format ( @_ )
{
if ( $link_format =~ m!^http://[_a-zA-Z0-9-]+\.[_a-zA-Z0-9-]+[.a-zA-Z0-9-]*(/~|/?)[/_.a-zA-Z0-9#?&=+-]*$!i) {
return 1;
}
else
{
return 0;
}
}
} |
Partager