如何使用Perl从自动下载链接下载文件

我试图从LWP :: Simple使用这个代码使用getstore下载一个excel文件:

use 5.010; use strict; use warnings; use LWP::Simple qw(getstore); $xls_link = "http://linksample.com/this/link/auto-downloads/when-link-is-entered.xls"; $file_dir = "file/directory/filename.xls" getstore($xls_link, $file_dir); 

出于某种原因,该文件没有下载。 我以前用图像使用LWP :: Simple,他们工作得很好。 他们与此唯一的区别是,它是一个Excel文件,并且当您inputURL时,URL自动下载文件。

而不是getstore我使用WWW :: Mechanize,而且我也从链接获取authentication错误

 #!/usr/bin/perl use 5.010; use strict; use WWW::Mechanize; use IO::Socket::SSL qw(); my $xls_link = "http://linksample.com/this/link/auto-downloads/when-link-is-entered.xls"; my $file_dir = "file/directory/filename.xls" my $mech = WWW::Mechanize->new(ssl_opts => { SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, verify_hostname => 0, # this key is likely going to be removed in future LWP >6.04 }); $mech->get("$xls_link"); $mech->save_content("$file_dir"); 

未经testing可能对您的请求有帮助。

 use strict; use warnings; use LWP::Simple; use WWW::Mechanize; my $xls_link = $ARGV[0]; #http://linksample.com/this/link/auto-downloads/when-link-is-entered.xls if (! head($xls_link)) { print "\nError: Provided weblink not available...!\n"; exit; } else { my $file_dir = "c:/file/directory/filename.xls"; if(-e $file_dir) { print "\nError: Provided Directory/File found...!\n"; exit; } getstore($xls_link, $file_dir); }