开发者

Perl save file from website to desktop

开发者 https://www.devze.com 2023-02-10 15:20 出处:网络
I have this script below, where I want to download a pdf from a URL #!/usr/bin/perl use warnings; use strict;

I have this script below, where I want to download a pdf from a URL

#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;

my $save = "C:\\Users\\rahard\\Desktop\\";  
my $file = get 'http://locationoffile';

How can I save the pdf to the Desktop? (if I click the URL, it will pront me to save a random name file)

Thank you

/edit some sy开发者_高级运维ntax error in $save and edit $file location


Use open to open a file handle and print to it. Also note that each backslash in $save must be escaped.

my $save = "C:\\Users\\rahard\\Desktop\\";  
my $file = get 'http://file.pdf';

open( FILE, '>', $save . 'filename.pdf' ) or die $!;
binmode FILE;
print FILE $file;
close( FILE );


Could it be that the url is not the origin location of the pdf and you get redirected to them? If the response of get is not a valid pdf, it could be the html-content of the redirecting page.

I would test this first.

Therefor I would use WWW::Mechanize or a similar library.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号