Test:
Take a keyword from STDIN and print top 5 google result for that keyword
Solution:
use WWW::Mechanize::Link;
print "Enter the keyword\n";
chomp($key=<STDIN>);
my $m = WWW::Mechanize->new();
$m->get("http://www.google.com");
$m->submit_form(
form_number => 1,
fields => { q => $key},
);
$m->click_button(name=>"btnG");
$c=$m->content();
(@links)=$c =~ /href.*?>(.*?)<\/a>/g;
$count=1;
foreach(@links)
{
if($_ =~ /<b>/)
{
$count++;
$_ =~ s/<b>//g;
$_ =~ s/<\/b>//g;
print $_;
print "\n\n";
if($count>5)
{
exit;
}
}
}

No comments:
Post a Comment