| imho.ws |
![]() |
|
|
|
# 1 |
|
Junior Member
Регистрация: 17.09.2005
Адрес: St. Petersburg
Сообщения: 118
![]() |
парсер гугле на перл
вот код парсера
<code> #! /usr/bin/perl use IO::Socket::INET; $server = "google.com"; $port = 80; $count = 0; $search = ""; @found = (); @dn = (); if((@ARGV < 2)||(@ARGV > 8)||(@ARGV % 2 > 0)) { usage(); exit; } $i = 0; while($i < @ARGV) { if($ARGV[$i] eq "-s"){ $server = $ARGV[$i+1]; } elsif($ARGV[$i] eq "-p"){ $port = $ARGV[$i+1]; } elsif($ARGV[$i] eq "-r"){ $search = $ARGV[$i+1]; } elsif($ARGV[$i] eq "-n"){ $count = $ARGV[$i+1]; } else { print "Invalid key: ".$ARGV[$i]."\n"; exit; } $i += 2; } if($search eq ""){ usage(); exit; } $search =~ s/(.)/sprintf("%%%02x",ord($1))/eg; if($server !~ /:[0-9]{2,5}$/){$server.=":$port";} for($i = 0; $i < 10; $i++) { @temp = get_request($server,"search?filter=0&num=100&start=".$i. "00&q=$search") =~ /(https?\:\/\/[a-z0-9\.\-\/\?\:\&\%\=\_]{5,})/gi; $a = 0; foreach $url (@temp) { if($url =~ /https?\:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/search\?q=cache:/i){ next; } ($domain) = $url =~ /^https?\:\/\/([a-z0-9\.\-]{5,})/i; $f=0;foreach(@dn){if($_ eq $domain){$f++;last;}}if($f){next;} push(@found, $url); if($count){if(!--$count){$a++;last;}} push(@dn,$domain); } if($a){last;} } foreach(@found){print "$_\n";} sub get_request() { local $sock; local $data = ""; local($server, $url) = ($_[0], $_[1]); $sock = IO::Socket::INET->new($server) or return -2; # connection failed print $sock "GET /$url HTTP/1.0\r\n\r\n"; while(<$sock>){$data .= $_;} close $sock; return $data; } sub usage() { print qq( drmist's google parser v 0.8 usage: gparse.pl [-s <server>] [-p <port>] -r <request> [-n <number>] <server> google server, default: google.com <port> http-server port, default: 80 <requets> search request <number> number of links, that will be shown, default: all examples: perl gparse.pl -r 'filetype hp inurl age=' | grep page=perl gparse.pl -s google.ru:80 -r 'Powered by phpbb' -n 10 perl gparse.pl -s google.ru -p 80 -r 'site:com inurl:backup.sql' perl gparse.pl -s google.de -r 'inurl:seite=' -n 400 ); } </code> вопрос в следующем. Как сделать чтобы скрипт делал запросы к гугле с определенного ip моего сервера. Я потом допишу чтобы запросы делались с разных ипов.
__________________
каждому по вере |
|
|
|
|
# 2 |
|
Full Member
Регистрация: 06.03.2003
Адрес: Earth
Сообщения: 761
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
смахивает на велосипед .
есть же модули : LWP HTML::Parser вот код (не мой , идет с пакетом дебиана libhtml-parser-perl) Код:
#!/usr/bin/perl -w
# This program will print out all <a href=".."> links in a
# document together with the text that goes with it.
use HTML::Parser;
my $p = HTML::Parser->new(api_version => 3,
start_h => [\&a_start_handler, "self,tagname,attr"],
report_tags => [qw(a img)],
);
$p->parse_file(shift || die) || die $!;
sub a_start_handler
{
my($self, $tag, $attr) = @_;
return unless $tag eq "a";
return unless exists $attr->{href};
print "A $attr->{href}\n";
$self->handler(text => [], '@{dtext}' );
$self->handler(start => \&img_handler);
$self->handler(end => \&a_end_handler, "self,tagname");
}
sub img_handler
{
my($self, $tag, $attr) = @_;
return unless $tag eq "img";
push(@{$self->handler("text")}, $attr->{alt} || "[IMG]");
}
sub a_end_handler
{
my($self, $tag) = @_;
my $text = join("", @{$self->handler("text")});
$text =~ s/^\s+//;
$text =~ s/\s+$//;
$text =~ s/\s+/ /g;
print "T $text\n";
$self->handler("text", undef);
$self->handler("start", \&a_start_handler);
$self->handler("end", undef);
}
добавлено через 1 минуту еще пример LWP use strict; use LWP::UserAgent; use URI::URL; my $ua = LWP::UserAgent->new; my $res = $ua->request(HTTP::Request->new(GET => $url)); print $res->content;
__________________
Смерть фашистским оккупантам. |
|
|