Esteemed Monks -
I am currently writing a simple script based on the
Mechnize module to download a bunch of PDFs from a website. The problem I am encountering is that the site seems to contain a link which points to a document that is unavailable on the server. As a result, the GET method returns the following error:
Error GETing <URL>: Not Found at extract.pl line 86
I have tried to incorporate robust handling of such error messages like this:
sub download()
{
my $doc = shift @_;
my $mech = WWW::Mechanize -> new();
return unless defined( $mech -> get( $doc ) );
my $link = $mech -> find_link( url_regex => qr/\.pdf/ );
return unless defined( $link );
$link = $link -> url_abs;
return unless ( $mech -> get ( $link ) ); # This is the GET oper
+ation which fails.
my $name = $1 if $link =~/.+\/(.+\.pdf)/;
$mech -> save_content( $name );
}
Unfortunately, the script still aborts as soon as the broken link is encountered.
Can you please advise how I can modify my code in order for the script to continue downloading even when broken links are hit upon?
Thanks in advance and best regards -
Pat
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.