如何在没有退出失败的下载的情况下下载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";
答案 0 :(得分: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";
答案 1 :(得分:4)
#!/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
块。
答案 2 :(得分:1)
是否因下载失败而死亡?如果是这样,请尝试使用eval
包装该调用'get'..