0

I read a question you asked about 5months ago on stackoverflow.com more precisly here it is: Using WWW::Mechanize to navigate forms on Amazon site

I am creating a script that enters a site and enters my credentials, to finally save the sites source code so I can parse the information.

I have a problem, my script works totally fine when used as a .pl script or on eclipse. But once I package it into a .exe it does not function. I noticed that it is related to my site type, more precisely any sites needing credentials i cannot package them into an functioning executable. Would you by any chance have an idea what the problem might be?

thank you very much!

here is my code:

#!/usr/local/bin/perl

use Spreadsheet::WriteExcel;
use WWW::Mechanize;

use Win32::Registry;
use LWP::Protocol::https;
use LWP;
use HTTP::Cookies;
use HTTP::Server::Simple;
use Net::HTTP;
use Pod::Usage;
use HTTP::Status;
use HTML::Form;
use Bundle::WWW::Mechanize::Shell;

# kills cmd prompt when .exe used on win32
BEGIN {
  if ($^O eq 'MSWin32') {
    require Win32::Console;
    Win32::Console::Free( );
  }
}

# Create a new instance of Mechanize
my $bot = WWW::Mechanize->new();

$bot->agent_alias( 'Windows Mozilla' );


# Connect to the login page
my $response = $bot->get('https://siteWithCredentials.com/' );
die "GET failed url" unless $response->is_success;

# Get the login form. You might need to change the number.
$bot->form_number(3);

# Enter the login credentials.
$bot->field( username => 'a username' );
$bot->field( password => 'a password' );
$response = $bot->click();

$bot->get('http://sitewithCredentials/directoryIamParsing.html' );
my $content = $bot->content();

my $outfile = "out.txt";
open(OUTFILE, ">$outfile");
print OUTFILE  $content;
close(OUTFILE);

open(FILE,$outfile);
my @releasesAU;
my @releasesAU3G;


while (<FILE>) {
    chomp;
    my $lineDATA = $_;
        if(index($lineDATA, "HN+_US_AU3G") != -1){

            if( $lineDATA =~ /">([_+\w]*)<\/a>/){
                print $1, "\n";
                push(@releasesAU3G,$1);
            }
        }

        if(index($lineDATA, "HN+R_US_AU") != -1){

            if( $lineDATA =~ /">([_+\w]*)<\/a>/){
                print $1, "\n";
                push(@releasesAU,$1);
            }
        }   
}
close(FILE);

my $row = 0;
my $col=0;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
$worksheet->write($row,  $col, "Releases HN+R_US_AU3G");
$worksheet->write($row,  $col+1, "Releases HN+R_US_AU3G");
$row=2;

foreach my $SOP (@releasesAU){
    $worksheet->write($row,   $col, $SOP);
    $row = $row+1;
}
$row =2;

foreach my $SOP (@releasesAU3G){
    $worksheet->write($row,   $col+1, $SOP);
    $row = $row+1;
}   

$workbook->close();
Community
  • 1
  • 1

1 Answers1

0

Suggestions:
1) Is the "require" line where the error is reported?
Since you know you are going to compile into a windows exe, the test MSWin32 is redundant. Remove the test and convert the "require" to a "use" statement to see if that helps.
2) Please update your question with any error messages.
3) PERL 101 - use strict and warnings.
4) Add code to check the results of statements that can fail, e.g. open, print and close.
5) Is the writing of the file contents really needed? Replace <FILE> with a split on $content
6) Type-globs (e.g. FILE, OUTFILE) are discouraged. Try package FileHandle.

null
  • 889
  • 1
  • 13
  • 24
  • Thank you nslntmnx, I had already taken : BEGIN { if ($^O eq 'MSWin32') { require Win32::Console; Win32::Console::Free( ); } } out of the code, it does not change anything. BAfter debugging, I noticed that when packaged to a .exe my script **works** for sites that do not ask credentials. It is only when the html page requires authentification, It stops at the first line where I call: my $response = $bot->get('https://siteWithCredentials.com/' ); Does this give you any further ideas where the problem resides. NOTE:when i run it as script it works! – user1757842 Oct 19 '12 at 06:59