万维网::机械化perl的,可执行

问题描述:

我看你问5个月前就stackoverflow.com更多precisly这里的一个问题是: Using WWW::Mechanize to navigate forms on Amazon site万维网::机械化perl的,可执行

我创建进入一个网站,并进入我的凭据,到一个脚本最后保存网站源代码,以便我可以解析信息。

我有一个问题,我的脚本在用作.pl脚本或eclipse时完全正常。但是,一旦我将它打包成.exe文件,它就不起作用。我注意到它与我的网站类型有关,更确切地说,任何需要凭据的网站我都无法将它们打包到正在运行的可执行文件中。 你有没有想过这个问题可能是什么?

非常感谢你!

这里是我的代码:

#!/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(); 

建议:
1)是 “需要” 行,其中报告错误?
既然你知道你要编译成一个Windows EXE,测试MSWIN32是多余的。 删除测试并将“require”转换为“use”语句以查看是否有帮助。 2)请更新您的问题与任何错误消息。
3)PERL 101 - 使用严格和警告。
4)添加代码以检查可能失败的语句的结果,例如,打开,打印和关闭。
5)文件内容的写作是否真的需要?替换<FILE>并在$ content上分割
6)不鼓励类型小节(例如FILE,OUTFILE)。尝试打包FileHandle。

+0

谢谢nslntmnx, 我已经采取了: BEGIN { 如果($^o EQ 'MSWin32'){ 需要的Win32 ::控制台; Win32 :: Console :: Free(); } } 出了代码,它不会改变任何东西。 调试完成后,我注意到打包到.exe的脚本**对不需要凭据的站点起作用**。只有当html页面需要认证时,它才会停在我呼叫的第一行: my $ response = $ bot-> get('https://siteWithCredentials.com/'); 这是否给你任何进一步的想法问题所在。注意:当我运行它作为脚本它的作品! – user1757842