got_quail has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm new to Perl (and perlmonks), so hopefully this isn't embarassing:

I'm looking for a way to pop up the resulting webpage of a search I have defined in my script.

I'm using LWP to input my search parameters and execute the search too, but what I get back is the HTML code corresponding to the result page. I want to actually launch that page from the search engine server to retain the formatting and everything. Basically I want to request the page and give it my input parameters at the same time.

Also, for some search engines I've tried I'm getting null results for valid search parameters, so I'm wondering if it's even possible to use LWP requests for the site I have in mind, but I guess that is a whole other question.

The following is the example I'm following, from CPAN:

use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); # Create a request my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search' +); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }

Replies are listed 'Best First'.
Re: Automated Browsing
by dasgar (Priest) on Jan 28, 2011 at 03:09 UTC

    It's my understanding that LWP is good for grabbing a single web page (or file), but not for automating interaction with web pages like what you're wanting to do.

    I believe that you'll probably want to use WWW::Mechanize. That module is used "for stateful programmatic web browsing, used for automating interaction with websites", which sounds like what you're trying to do. If you're needing JavaScript support, I believe that WWW:Mechanize::Firefox would enable support for JavaScripts.

Re: Automated Browsing
by Marshall (Canon) on Jan 28, 2011 at 04:51 UTC
    Well, the code that you have is indeed working. You are getting back the web page just like you had typed in the URL: http://search.cpan.org/search?query=libwww-perl&mode=dist . The script could have just done a GET on that URL. Yes, you get the html page. If you save it, you can open it with say Firefox.

    I think there is mis-understanding here: "pop up the resulting webpage". LWP and Mechanize have nothing to do with displaying a webpage. That's what browsers do. If what you really mean is that you want to control what Firefox does from Perl, then I think you want WWW::Mechanize::Firefox. It is this display requirement that LWP and Mechanize alone can not do.

Re: Automated Browsing
by zentara (Cardinal) on Jan 28, 2011 at 12:00 UTC
Re: Automated Browsing
by got_quail (Initiate) on Jan 28, 2011 at 21:10 UTC

    Thanks a lot for the replies! I got the functionality I wanted using WWW::Mechanize::Firefox.