Perl WWW::Mechanize methods not working in AIX

旧时模样 提交于 2019-12-11 08:09:11

问题


I have a simple requirement of screen scraping a web-page (simple URL based reports) and direct the HTML response to an output file. The URL will however redirect to an authentication (HTTPS Login) page with "form based" authentication (no javascript) and upon authentication the report I am trying to view should show up in the $response (as HTML). Interestingly, my code is working just fine in a Windows machine, however the same code below is not working in AIX machine and it looks like the click_button() function call does nothing. I have tried click(), submit(), but none is working so instead of getting the actual report all I get is the logon screen in the HTML output file. Any ideas, what can be wrong?

use WWW::Mechanize;
use strict;

my $username = "admin";
my $password = "welcome1";  
my $outpath  = "/home/data/output";
my $fromday = 7;
my $url  = "https://www.myreports.com/tax_report.php";
my $name = "tax_report";
my $outfile = "$outpath/$name.html";

my $mech = WWW::Mechanize->new(noproxy =>'0');  

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900; 
$mon++;     # since it will start from 0
$mday--;    # yesterdays date (to day)
$fromday = $mday - $days; #(from day)

#Create URL extension for generating report with previous date
my $dt_range = "?Y&dc_date1=$mon%2F$fromday%2F$year&dc_date2=$mon%2F$mday%2F$year"; 
my $url  = $url . $dt_range;

$mech->get($url);
$mech->field(login => "$username");
$mech->field(passwd => "$password");

$mech->add_handler("request_send",  sub { shift->dump; return });
$mech->add_handler("response_done", sub { shift->dump; return });

$mech->click_button(value=>"Login now");

my $response = $mech->content();

print "Generating report: $name...\n";

open (OUT, ">>$outfile")|| die "Cannot create report file $outfile";
print OUT "$response";
close OUT;

The WWW::Mechanize version in both the Machines are same i.e. 1.54 but the Win machine perl version is 5.10.1 whereas the AIX machine's Perl version is 5.8.8.

Other Alternatives Used -

my $inputobject=$mech->current_form()->find_input( undef,'submit' );
print $inputobject->value . "\n";
$mech->click_button(input => $inputobject);
print $mech->status() . "\n";

The $inputobject shows the correct button element as in the HTML source and the second print returns a status of 200 which apparently stands for OK. But its still not working in the AIX machine.

UPDATE- It seems that the site I am trying to connect to has an un-trusted SSL certificate. I tried the program on three different machines Windows PC, Mac and AIX. On the Windows Machine the program works and I was able to login to the website through the browsers (Chrome, Firefox,IE). However in Mac, the login just won't work (through the browsers) and it shows an un-trusted certificate error (or warning!) this probably means the proxy settings are not set up, the Perl program won't work either. And lastly the AIX where the Perl is not working as well. Not sure how to bypass this un-trusted SSL certificate issue here. Any help will be appreciated.

UPDATE2: Included below lines of code in the script to see logging details and found that I was being re-directed (HTTP 302) since my IP was filtered by the server Firewall. Once the AIX ip was added to the server's firewall exception the script worked perfectly. The two lines below were the life saver-

$mech->add_handler("request_send",  sub { shift->dump; return });
$mech->add_handler("response_done", sub { shift->dump; return });

回答1:


Can you use the following line before my $mech = WWW::Mechanize->new(noproxy =>'0'); of your perl code and try again ?

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;


来源:https://stackoverflow.com/questions/11422917/perl-wwwmechanize-methods-not-working-in-aix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!