Matthew.S has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks!!

I'm looking for a way to download a file that is auto-generated upon a request. As such, the path to it is not always known.

Via Firefox, I hit a link (that is static and always known) and once the file is ready for download the browser pops a window saying "What should Firefox do with this file?". Choose "Save File" hit "OK" and I'm done.

I'd like to automate this process. I bet (or should I say hope) that there is a neat Perl module that is waiting for me to grab it, but I just can't find it.

To be more concrete, it's a spreadsheet from "Google finance" that I'm trying to download: "http://www.google.com/finance/historical?q=NASDAQ:FFIV&output=csv"

Please help, Thanks in advance,

Matthew.S.

Replies are listed 'Best First'.
Re: Downloading a auto-generated file.
by Corion (Patriarch) on Jan 27, 2011 at 23:06 UTC

    WWW::Mechanize allows you to simulate a browser from within Perl. Most likely, you'll find the appropriate ways to navigate the web page and to ->click on the link.

    If you need Javascript support, then maybe Win32::IEAutomation or WWW::Mechanize::Firefox can help you get further.

Re: Downloading a auto-generated file.
by wind (Priest) on Jan 27, 2011 at 23:09 UTC
    All you need is LWP::Simple,
    use strict; my $url = 'http://www.google.com/finance/historical?q=NASDAQ:FFIV&outp +ut=csv'; my $file = 'data.csv'; use LWP::Simple; my $content = get($url); die "Couldn't get it!" unless defined $content; open my $fh, '>', $file or die "Can't open file $file: $!"; print $fh $content; close $fh;
    - Miller

      Thank you for this!!!

      This is simple and beautiful!!!

      Matthew.

      It might be clearer to use FH instead of $fh...

      open FH, '>', $file or die "Can't open file $file: $!"; print FH $content; close FH;

        Clearer for those accustomed to old school Perl and in a very small context maybe.

        The preferred convention currently is to use a lexical file handle as shown by wind which is safer than the old school bare word file handle and, with an appropriate name, is at least as clear.

        True laziness is hard work