Wednesday, 15 October 2014

Test 19: Enter a keyword(product name) in google and find the company which offers that product at minimum cost in google result

Test:
Take a keyword(Product name) from STDIN and find the prices and companies name displayed by google result. print the minimum price and company. If item not found, print - no shoppable item

Learning: 
get content and use regex. 



Solution:

use WWW::Mechanize;
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();

print ABC $c;

if($c !~ /Shop for .*? on Google/)
{
print "no shoppable item\n";
exit;
}

(@links)=$c =~ /(Rs\.\s.*?)<.*?wrap\">(.*?)<\/cite>/g;


$len=@links;
@price=();
for($i=0;$i<$len;$i++)
{

$links[$i] =~ s/Rs\..*?(\d)/Rs\.$1/;
print "$links[$i] $links[$i+1]" ;
$links[$i] =~ s/Rs\.//;
$links[$i] =~ s/,//;
push(@price,$links[$i]);
$i++;
print "\n";
}
@price=sort{$a<=>$b}@price;
$min=$price[0];
print "\nLowest price is $min by ";
for($i=0;$i<$len;$i++)
{
if($links[$i] =~ /$min/)
{
print "$links[$i+1]\n";
exit;
}
$i++;
}

output snapshot:

No comments:

Post a Comment