Friday, 17 October 2014

Test 20: Find name of director of movie from wikipedia website

Test:
Given a wikipedia page for any movie. Find the name of director of movie.

Learning: 
use of Mojo::DOM to read text of any tag
use /regex/s to consider space also, like /By director/ wont work but /By director/s will work



Solution:

use WWW::Mechanize;
use WWW::Mechanize::Link;
use Mojo::DOM;

my $m = WWW::Mechanize->new();
$m->get("http://en.wikipedia.org/wiki/Kal_Ho_Naa_Ho");


$c=$m->content();

(my $a) = $c =~ /Directed by(.*?\/a>)/s;

my $dom = Mojo::DOM->new($a);
print $dom->at('a')->text;

output snapshot:

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:

Tuesday, 14 October 2014

Test 18: Put a keyword to google and find top 5 results

Test:
Take a keyword from STDIN and print top 5 google result for that keyword

Learning: 
get content and use regex. Google uses <b> for keywords



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();


(@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;
}
}
}

output snapshot:

Wednesday, 8 October 2014

Test 17: Print ALT text and URL of all images in a page

Test:
Get all images of a page and print ALT text and URL

Learning: 
$m->images() is used for images



Solution:

use WWW::Mechanize;

my $m = WWW::Mechanize->new();
$m->get("http://vienna.yapceurope.org/ye2007/search");
@arr=$m->images();
$count=1;
foreach(@arr)
{
print "Image $count \n";
$tmp=$_->alt;
print "ALT is $tmp\n";
$tmp=$_->url;
print "URL is $tmp\n\n";
$count++;
}



output snapshot:

Test 16: show various direct methods with Mech

Test:
Use the methods which can be directly used with $m->

Learning: 
Various direct methods with $m->



Solution:

use strict;
use warnings;

use WWW::Mechanize;

my $m = WWW::Mechanize->new();
$m->get("http://www.rediff.com");

print "URL is- ".$m->uri()."\n";

print "Status code is- ".$m->status()."\n";

print "Content type is- ".$m->content_type()."\n";

print "Base URI is- ".$m->base()."\n";

print "is content HTML- ".$m->is_html()."\n";

print "title is- ".$m->title()."\n";


output snapshot:

Test 15 :Set up new user agent and verify the request is sent via that user agent

Test:
Set up new user agent and verify the request is sent via that user agent

Learning: 
$mech->agent_alias( 'Windows IE 6' ); is used to set user agent.

possible UA can be:
  • Windows IE 6
  • Windows Mozilla
  • Mac Safari
  • Mac Mozilla
  • Linux Mozilla
  • Linux Konqueror


Solution:

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->agent_alias( 'Windows IE 6' );
$mech->get('http://whatsmyuseragent.com/');
my $data = $mech->content;

(my $ua)= $data =~ /<h2 class="info">(.*)<\/h2>/;

print $ua;


output snapshot:

Test 14: Print values of form fields, update them and print new values

Test:
Get values of all fields of a form, update the values and then print new values

Learning: 
$form->value used to get values


Solution:

use strict;
use warnings;

use WWW::Mechanize;

my $m = WWW::Mechanize->new();
$m->get("http://vienna.yapceurope.org/ye2007/search");

my $count = 1;
for my $form ($m->forms) 
{
    print "form $count fields are:\n";
    $count++;
    for ($form->param) 
{
        printf "%s - %s\n", $_, $form->value($_);
    }
    print "\n";
}

$m->submit_form(
    form_number => 1,
    fields    => { name  => 'honey', town => 'Delhi', country =>

'india', pm_group => 'Cam.pm'},
);

$count = 1;
print "After filling data to form\n";
for my $form ($m->forms) 
{
    print "form $count fields are:\n";
    $count++;
    for ($form->param)
{
        printf "%s - %s\n", $_, $form->value($_);
    }
    print "\n";
}


output snapshot: