-2

Hello everyone I coded a script to search a string on a webpage but the request doesn't works I don't know why ...

website : http://www.matrixx.com/ string to search : solutions

code :

#!/usr/bin/perl
use strict;
use IO::Socket;
use Term::ANSIColor;
use HTML::Parser;
use LWP::UserAgent;
use LWP::Simple;
use vars qw( $PROG );

$SIG{'INT'} = sub {exit;};

my $stringsearch = "solutions";

my $url = "http://www.matrixx.com/";
my $ua = LWP::UserAgent->new;
print "\e[96m[!]Searching \e[31m$url\n\e[0m";    
my $response = $ua->post($url);
if ( !$response->is_success ) 
{
 print "error\n";
}


my $parser = HTML::Parser->new( 'text_h' => [ \&text_handler, 'dtext' ] );
$parser->parse( $response->decoded_content );

sub text_handler 
{
    chomp( my $text = shift );
    if ( $text =~ /$stringsearch/i )
    {

        print "\e[96m[+]Found: \e[32m$url\e[0m\n";

    }

    else
    {
        print "Not Found \n";
    }
}
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
toto
  • 1
  • Questions of the type "Why does this code not work" are off topic for stackoverflow. You need to ask specific coding questions, about your code. And also, `use warnings` is quite essential when debugging. – TLP Jan 04 '15 at 17:02
  • When posting to others, ensure to strip off all the unneeded stuff that's only noise and **indent your code** properly as I do partially. – Gilles Quénot Jan 04 '15 at 17:40

2 Answers2

3

In quite a few lines & using :

#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTML::TreeBuilder::XPath;

my $stringsearch = "solutions";

my $url = "http://www.matrixx.com/";
my $ua = LWP::UserAgent->new;
my $response = $ua->get($url);
die "Http error\n" unless $response->is_success;

my $tree = HTML::TreeBuilder::XPath->new_from_content(
    $response->decoded_content
);

print "searched string found\n" if $tree->exists(
  "//*[contains(name(), '$stringsearch')] | //@*[contains(., '$stringsearch')]"
);
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

text_handler is called for every snippet of text on the page. It does indeed find your search string, but only in one of them; you are printing Not Found for all the others.

If you only want to print Found or Not Found once per url, do this:

my $found;
my $text_handler = sub {
    chomp( my $text = shift );
    if ( $text =~ /$stringsearch/i ) {
        $found = 1;
    }
};
my $parser = HTML::Parser->new( 'text_h' => [ $text_handler, 'dtext' ] );
$parser->parse( $response->decoded_content );
if ($found) {
    print "\e[96m[+]Found: \e[32m$url\e[0m\n";
}
else {
    print "Not Found\n";
}

(If this doesn't answer your question, please be more explicit about what you are seeing and how that differs from what you expect to see.)

ysth
  • 96,171
  • 6
  • 121
  • 214