繁体   English   中英

www :: mechanize perl,可执行

[英]www::mechanize perl, executable

我在5个月前在stackoverflow.com上更仔细地阅读了您问的一个问题,它是: 使用WWW :: Mechanize导航亚马逊网站上的表单

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

我有一个问题,当用作.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)是否在“ require”行中报告了错误?
由于您知道要编译为Windows exe,因此测试MSWin32是多余的。 删除测试,然后将“ require”转换为“ use”语句,以查看是否有帮助。
2)请使用任何错误消息更新您的问题。
3)PERL 101-使用严格的警告语。
4)添加代码以检查可能失败的语句的结果,例如打开,打印和关闭。
5)是否真的需要编写文件内容? 将<FILE>替换为$ content上的拆分
6)不建议使用类型团(例如FILE,OUTFILE)。 尝试打包FileHandle。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM