如何下载文件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";
您可以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";
#!/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
如果您只想消除错误,请忽略该块。
它会因下载失败而死吗?eval
如果是这样,请尝试用..将该调用包装为“获取”