Corion, forget the JavaScript for now. The goal is to use a single method to download all type of files (images, pdfs, CSVs etc..)
According to the documentations, the following method should be able to do it.
my @links = $mech->find_all_links(url_regex => qr/\.pdf/i);
my @urls = map { $_->url_abs } @links;
foreach (my $foo @urls )
{
my $abs_path = "C:/path";
$mech->set_download_directory( $abs_path );
$mech->get($foo);
}
Unfortunately this method works only with files that the browser normally does not load such as (.csv). If you use this method with images (.jpg etc...) or PDFs (.pdf) the documents will load in the browser and the download fails.
This exact same task has worked with WWW::Mechanize using the following method
my $filename = "C:/path/filename";
my $foo = "http link";
$mech->get($foo, ':content_file'=>$filename);
The workaround that you have suggested below didn't work with PDFs and CSVs. it worked with images only.
my $response = $mech->get($url);
my $c = $mech->content; # Dummy request to initialize everything
open my $output, '>:raw', '/tmp/output.jpg';
print { $output } $response->decoded_content;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.