English 中文(简体)
How do I download a file with WWW::Mechanize after it submits a form?
原标题:

I have the code:

#!/usr/bin/perl
use strict;
use WWW::Mechanize;

my $url =  http://divxsubtitles.net/page_subtitleinformation.php?ID=111292 ;
my $m = WWW::Mechanize->new(autocheck => 1);
$m->get($url);
$m->form_number(2);
$m->click();
my $response = $m->res();
print $m->response->headers->as_string;

It submits the download button on the page, but I m not sure how to download the file which is sent back after the POST.

I m wanting a way to download this with wget if possible. I was thinking that their may be a secret url passed or something? Or will I have to download it with LWP directly from the response stream?

So how do I download the file that is in that header?

Thanks,

Cody Goodman

最佳回答

Well the thing that threw me off the most was the "mechanize->form_number" subroutine starts at 1 whereas typical programs start their index at 0. If anyone wants to know how to download response headers, or download header attachments, this is the way to do it.

Now here s the full code to do what I wanted.

#!/usr/bin/perl
use strict;
use WWW::Mechanize;

my $url =  http://divxsubtitles.net/page_subtitleinformation.php?ID=111292 ;
my $m = WWW::Mechanize->new(autocheck => 1);
$m->get($url);
$m->form_number(2);
$m->click();
my $response = $m->res();
my $filename = $response->filename;

if (! open ( FOUT, ">$filename" ) ) {
    die("Could not create file: $!" );
}
print( FOUT $m->response->content() );
close( FOUT );
问题回答

After submitting the form, you can use:

$mech->save_content( $filename )

Dumps the contents of $mech->content into $filename. $filename will be overwritten. Dies if there are any errors.

If the content type does not begin with "text/", then the content is saved in binary mode.

Source: http://metacpan.org/pod/WWW::Mechanize

I tried your code and it returns a stack of HTML of which the only http:// references were:

    http://www.w3c.org
    http://ad.z5x.net
    http://divxsubtitles.net
    http://feeds2read.net
    http://ad.z5x.net
    http://www.google-analytics.com
    http://cls.assoc-amazon.com
using the code

    my $content = $m->response->content();
    while ( $content =~ m{(http://[^/" 	

]+)}g ) {
        print( "$1
" );
    }

So my comments to you are:
1. add use strict; to your code, you are programming for failure if you don t
2. read the output HTML and determine what to do next, you haven t done that, and therefore you ve asked an incomplete question. Unless you identify the URL you want to download you are asking somebody else to write a program for you.

Once you ve identified the URL you want to download it is a simple matter of getting it and then writing the response content to a file. e.g.


if ( ! open( FOUT, ">output.bin" ) ) {
    die( "Could not create file: $!" );
}
binmode( FOUT ); # required for Windows
print( FOUT $m->response->content() );
close( FOUT );




相关问题
How to upload and download file from a server using C#

I am developing a webpage in that the user can download their Resume for Edit. So I have a link to download the article. I use the following code for download. DataTable dt = user.getUserDetails(2); ...

Detecting PDF Download

I have recently added functionality for generating pdf reports to a web application. However the reports can be quite large and take some time to show the pdf download dialog. I want to show a spinner ...

Writing file to users giving sporadic error in IE

I have a very interesting issue with only specific IE implementations. I have an ASPX page that is used to write files down to the user, as part of the process the page uses the following code to ...

PHP - List of Excel files to download from Intranet Site?

I have a intranet site running PHP 5 that needs to list a folder containing only Excel files. Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered ...

Determine total size of SVN directory/trunk

Is there a way to count/calculate the total size of a svn directory if you were to checkout a revision? I have limited internet downloads so I need to know how big something is before I go and ...

Scala and html: download an image (*.jpg, etc) to Hard drive

I ve got a Scala program that downloads and parses html. I got the links to the image files form the html, Now I need to transfer those images to my hard drive. I m wondering what the best Scala ...

Downloading file with wxHTTP?

Im really stumped. Im using the wxHTTP class in wxWidgets to try and download two files. The first request succeeds but the second one fails when wxHTTP->GetInputStream is called. Between downloads, ...

热门标签