1

如何下载文件WWW::Mechanize而不退出失败的下载?

#!/usr/bin/perl

use strict;
use WWW::Mechanize;

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

$mech->get("http://google.com/test", ':content_file' => "tmp");

print "done";
4

3 回答 3

6

您可以autocheck => 0在构造函数中使用:

#!/usr/bin/perl

use strict;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new(
    # perldoc WWW::Mechanize | less +/autocheck
    autocheck => 0
);

$mech->get("http://google.com/test", ':content_file' => "tmp");

# Now, need to check errors manually :
# test on the HTTP return code see :
# http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
my $retcode = $mech->status();
if ($retcode >= 400) {
    warn("error getting url, HTTP code: [$retcode]\n");
}

print "done\n";
于 2012-06-03T22:36:51.593 回答
4

使用Try::Tiny

#!/usr/bin/perl

use strict;
use WWW::Mechanize;
use Try::Tiny;

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

try {
    $mech->get("http://google.com/test", ':content_file' => "tmp");
}
catch {
    print "failed: $_";
}; # <-- do not forget the semicolon

print "done";

catch如果您只想消除错误,请忽略该块。

于 2012-06-03T22:32:44.643 回答
1

它会因下载失败而死吗?eval如果是这样,请尝试用..将该调用包装为“获取”

于 2012-06-03T21:36:48.953 回答