palsy017 has asked for the wisdom of the Perl Monks concerning the following question:

Good day. I have a script that uses WWW::Mechanize to download PDFs from a website. It works for downloading, but I need to save each file with a file name derived from the link that it came from.

Here is the code:

use URI::Escape; my $start = "http://www.xxxx.com/programs_results/"; my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->get( $start ); my @links = $mech->find_all_links( url_regex => qr/\d+.+\.pdf$/ ); chdir $progdest or die "Can't change directory: $!\n"; for my $link ( @links ) { my $url = $link->url_abs; my $filename = URI::Escape::uri_escape( $link- >url_abs->path ); $filename =~ s[^.+/][]; mech->get( $url, ':content_file' => $filename ); }

This code(listed above) works, but I need to make a change. The PDFs are saving with the encoding in the file name. How could this be filtered so that the end result looks like A, not B?

A. 100004_20090326.pdf

B. %2Fpdf%2F100004%2F_20090326.pdf


Thanks alot!!!

Replies are listed 'Best First'.
Re: Question about URI::Escape
by ikegami (Patriarch) on Mar 30, 2009 at 19:23 UTC
    Replace
    my $url = $link->url_abs; my $filename = URI::Escape::uri_escape( $link->url_abs->path ); $filename =~ s[^.+/][];
    with
    my $base = "http://www.xxxx.com/programs_results/pdf/"; my $filename = join '', $link->rel($base)->path_segments();
Re: Question about URI::Escape
by Anonymous Monk on Mar 31, 2009 at 06:32 UTC