Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

LWP Form scraping

by rinceWind (Monsignor)
on Jan 14, 2003 at 10:54 UTC ( [id://226795]=perlmeditation: print w/replies, xml ) Need Help??

I have been involved in an LWP exercise recently. Rather than trivial link spidering, this involved form filling and POST method.

I was struck with the feeling that boiling down the form data is something that has probably been done many times over - but a search didn't find anything obvious.

I feel a CPAN module coming on (unless it's already been done and I've missed it), but I'm stuck on which namespace to use: HTML::Formdata, HTTP::Formdata, LWP::HTMLForm, thoughts please.

From the existing code that I have written, is a sub formdata, which takes the HTML page and form name as parameters. The form name is optional; if no form name is specified, the routine picks up the first form on the page.
sub formdata { my ($html,$formname) = @_; my $tp = HTML::TokeParser->new(\$html) or die "Bad HTML form"; while (my $form = $tp->get_tag('form')) { last if !$formname || ($form->[1]{name} eq $formname); $tp->get_text('/form'); } my @form; while (my $field = $tp->get_tag('input','select','textarea')) { my ($tag,$attr) = @$field; if ($tag eq 'textarea') { my $text = $tp->get_text('/textarea'); push @form,$attr->{name},$text; next; } if ($tag eq 'select') { my $selected; while (my $tok = $tp->get_token) { last if $tok->[-1] =~ m(/select)i; my ($typ,$tag,$att) = @$tok; next unless $typ eq 'S' && $tag eq 'option'; $selected = $att->{value} if exists $att->{selected}; } push @form,$attr->{name},$selected if defined $selected; next; } if ($attr->{type} =~ /hidden|password|text/) { push @form,$attr->{name},$attr->{value}; } if ($attr->{type} =~ /radio|checkbox/ && exists $attr->{checked}) { push @form,$attr->{name},$attr->{value}; } } @form; }
The sub returns a list of key/value pairs. Thinking about it, I realised that if the calling code turns it into a hash, this could lose any duplicate keys.

At this point, the light of recognition came on in my mind. This was a very familiar concept, that of a CGI object. I could make formdata return a CGI object or something inheriting from CGI, giving access to all the input fields via $form->param.

Besides being capable of being submitted via a normal POST of encoding type application/x-www-form-urlencoded, I would also like the code to be able to handle file uploads and encoding type multipart/form-data.

Has anything like this been done before? Please let me know if I am duplicating effort here.

rinceWind

Replies are listed 'Best First'.
Re: LWP Form scraping
by adrianh (Chancellor) on Jan 14, 2003 at 11:03 UTC

    Take a look at WWW::Mechanize. Very nice little module. I think it does what you're after.

      see also the introduction of WWW:Mechanize in the 2002 Perl Advent Calendar here.

      -- Hofmator

Re: LWP Form scraping
by Koschei (Monk) on Jan 17, 2003 at 05:38 UTC
    Others mentioned WWW::Mechanize (based on, but improving on, WWW::Automate). WWW::Mech just happens to be a handy wrapper around a few other modules. In particular, the HTML::Form module. Check that out.
Re: LWP Form scraping (consider Webchat)
by grinder (Bishop) on Jan 14, 2003 at 13:49 UTC

    A different approach can be found in Webchat. It hasn't been updated in a while. I know BooK and OeufMayo were threatening at one stage to take over the development, but I think they're suffering from lack of tuits.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-24 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found