Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

use WWW::Mechanize or LWP to interface with WEB

by learnperl (Acolyte)
on Nov 16, 2007 at 02:27 UTC ( [id://651115]=perlquestion: print w/replies, xml ) Need Help??

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

I am developing a Perl script which takes user input and generate multiple outputs related to the user input. But What i want to do is that fill a certain website with the end results of my program. I read the documentation and fallowed some examples in WWW::Mechanize but I am still struggling to figure it out.. I have the fallowing the HTML and in that I have two fields that I would like to fill in with the results generated by my Perl script.
<div id="treeform" class="treeformWide"> <form> <div id="elementsContainer" class="clearfix"> <div class="formElement"> <label for="firstName">Your First Name</label> <input id="tree_fn" size="15" type="text"> </div> <div class="formElement"> <label for="lastName">Your Last Name</label> <input id="tree_ln" size="15" type="text"> </div> <div class="formElement"> <label for="gender">YourGender</label> <select id="tree_gen" name="gender"> <option selected="selected"value="M">Male</option> <option value="F">Female</option></select> </div> </div> <div style="text-align: center; clear: both; padding-top: 10px;"> <input name="start" class="p_btn" id="star +t" value="Get Started" onclick="submitInfo();s_objectID='hp_build_tre +e';return false;" type="submit"> </div> </form> </div>
Here is the part of the my code that use the WWW:Mechanize, My code has other parts which are not related to the WWW:Mechanize, I didn't put all the code since those are not relevant to WWW::Mechanize.
#!/usr/bin/perl use Text::Soundex; use LWP; use WWW::Mechanize; my $mechObject = WWW::Mechanize->new(); $mechObject -> get($url); $mechObject->field($queryString); $mechObject->click();
It will be a great help if someone could guide me on this.. Thanks in advance..

Replies are listed 'Best First'.
Re: use WWW::Mechanize or LWP to interface with WEB
by erroneousBollock (Curate) on Nov 16, 2007 at 04:19 UTC
    Something like this?

    use WWW::Mechanize; use strict; my $url = 'http://server/path/to/form.html'; my $mech = WWW::Mechanize->new(); $mech->get($url); die "can't get page!" unless $mech->success && $mech->content =~ /look for something/; $mech->submit_form(with_fields => { tree_fn => 'learn', tree_ln => 'perl', tree_gen => [ option => 'M' ] }); die "can't submit form!" unless $mech->success && $mech->content =~ /something useful/;

    (untested - nothing to test it against ;)

    -David

      Thanks alot David, One issue I had before was that the html code taken from the site that I am running against of (http://www.ancestry.com/) is using
      <input id="blabla"/> instead of <field name="blabla/>... How can we handle this issue. I tried the code that you submitted but I was getting a similar issue that I got before which was
      There is no form with the requested fields at D:/PerlWorkSpace/Research/Genealogy/gen.pl line 226 Died at D:/Perl/lib/WWW/Mechanize.pm line 1715, <STDIN> line 1.
        No field names, eh?

        Try something like this:

        use WWW::Mechanize; use strict; my $url = 'http://server/path/to/form.html'; my $mech = WWW::Mechanize->new(); $mech->get($url); die "can't get page!" unless $mech->success && $mech->content =~ /look for something/; die "can't find form!" unless $mech->form_number(0); my @values = ('learn', 'perl', [option => 'M']); die "couldn't fill out form!" unless $mech->set_visible(@values) == 3; $mech->submit; die "can't submit form!" unless $mech->success && $mech->content =~ /something useful/;

        Any better?

        -David

Re: use WWW::Mechanize or LWP to interface with WEB
by naikonta (Curate) on Nov 16, 2007 at 04:22 UTC
    You should select the form first to work on after the get() method. Saying $mechObject->form_number(1) means that work on the form number 1, which is the only form in the HTML. The field() method works on a single field, so you have to call it twice for to fill in two fields. The click() method returns HTTP::Response object so you may interested the clicking result or the content returned by the server.
    my $resp = $mechObject->click; die "Submit failed\n" if $resp->is_error; my $content = $resp->content; # do something with $content if necessary
    However, if you have problem due the the JavaScript in the HTML source, check mechanize and javascript.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://651115]
Approved by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found