这次作业的要求是写一个简单的网络爬虫程序,我用perl写的。做了一个简陋的图形界面,其实就一个窗口里有个按钮和一个文本框,由于时间问题就没再完善了。很简陋的,改进的留待下次作业
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTML::LinkExtor;
use Tk;
my %m;
my $m;
my $count=0;
my $k="";
my $input;
open OUT,">file.txt";
sub one {
my($url)=@_;
$count++;
my $browser=LWP::UserAgent->new;
$browser->agent("Mozilla/5.0");
my $response=$browser->get($url);
print OUT $response->content();
my $link_extor=HTML::LinkExtor->new(\&handle_links);
$link_extor->parse($response->content);
sub handle_links {
my($tag,%links)=@_;
my $key;
if ($tag eq 'a') {
foreach $key (keys %links) {
if ($key eq 'href') {
if ($links{$key}=~m/^http:\/\//) {
if (!(exists $m{$links{$key}})) {
$m{$links{$key}}='1';
if ($count<4) {
one($links{$key});
}
}
}
}
}
}
}
}
my $main = MainWindow->new;
$main->Button(-text=>'Click Me!',
-command=>\&display
)->pack(-side=>"top");
my $text1=$main->Text('-width'=>50,'-height'=>50
)->pack;
one($k);
sub display {
$text1->delete('1.0','end');
foreach $m (keys %m){
$text1->insert('end',"$m\n");
}
}
MainLoop;
|
阅读(750) | 评论(0) | 转发(0) |