简体   繁体   中英

Perl WWW::Mechanize from string variable

In Perl, using module WWW::Mechanize (required, not other module), is it possible to "parse" document from string variable, instead of url?

I mean instead of

       $mech->get($url);

to do something like

       $html = '<html...';
       $mech->???($html);

Possible?

You could write the data to disk and then get() it in the usual manner. Something like this:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Temp;
use URI::File;
use WWW::Mechanize;

my $data = '<html><body>foo</body></html>';

# write the data to disk
my $fh = File::Temp->new;
print $fh $data;
$fh->close;

my $mech = WWW::Mechanize->new;
$mech->get( URI::file->new( $fh->filename ) );

print $mech->content;

prints: <html><body>foo</body></html>

Got it:

          $mech->get(0);
          $mech->update_html('<html>...</html>');

It works!

Not really. You could try getting the HTTP::Response object using $mech->response and then using that object's content method to replace the content with your own string. But you would have to adjust all the message headers as well and it would get quite messy.

What is it that you want to do? The methods like forms and images that WWW::Mechanize provides are based on other modules and are fairly simple to code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM