Perl, resolving link download name

折月煮酒 提交于 2019-12-08 01:01:53

问题


I am trying to write a script in Perl that generates URL list from web page, than goes over URL list and downloads the linked files.

the problem is that the generated URL's links look something like

www.example.com/download.php?file=Li9OaW50ZW5kby9ORVMvVVNBLzEwIFlhcmQgRmlnaHQuemlw

when i place this link in browser it knows the true file name (i.e. cards.zip), i don't know how to get it using Perl.

how can i get using Perl the link's 'true' file name ?


回答1:


Inspect the headers, specifically Content-Disposition.




回答2:


Amadan had it right, just showing the code i used to solve it:

use LWP::UserAgent qw( );
use LWP::Simple;
$url  = 'http://www.example.com/download.php?file=Li9OaW50ZW5kby9ORVMvVVNBLzNEIEJhdHRsZXMgb2YgV29ybGQgUnVubmVyLnppcA==';

#get file name
my $ua = LWP::UserAgent->new();
$cnt = %{%{$ua->head( $url )}->{'_headers'}}->{'content-disposition'};
$cnt =~ m/filename=(.*)/;
print "File name is: $1\n";

#Save File
my $status = getstore($url, $1);

if ( is_success($status) ){
    print "File $1 Saved Correctly !\n";
}else{
    print "Error saving $1!\n";
}


来源:https://stackoverflow.com/questions/8782813/perl-resolving-link-download-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!