in reply to use WWW::Mechanize or LWP to interface with WEB

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

Replies are listed 'Best First'.
Re^2: use WWW::Mechanize or LWP to interface with WEB
by learnperl (Acolyte) on Nov 16, 2007 at 06:35 UTC
    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

        Thanks David,
        Good news and not so good news, I don't get the error anymore but I nothing happens either, I had the feeling that WWW::Mechanize should fire up the web browser... Have I misunderstood...?
        One other thing, If you take look at source of $url the website has a "form tag" before the form that I want to use... in that case should be using (2)..?
        my $url = 'http://www.ancestry.com/'; my $mechObject = WWW::Mechanize->new(); $mechObject->get($url); die "can't get page!" unless $mechObject->success; die "can't find form!" unless $mechObject->form_number(0); my @values = ('learn', 'perl', [option => 'M']); die "couldn't fill out form!" unless $mechObject->set_visible(@values) == 3; $mechObject->submit; die "can't submit form!" unless $mechObject->success;
        learnperl