An example program using HTML::TokeParser to do what you need might look like the following - This is based on some of my own code for XML generation from HTML-parsed sources and allows for the definition (and separation of elements therein) of multiple forms in a HTML document:
#!/usr/bin/perl -Tw use HTML::TokeParser; use LWP::Simple; use XML::Simple; use strict; my $html = get('http://www.perlmonks.org/'); die "LWP::Simple failed to retrieve source HTML - $!" unless ($html); my (%data, $formname, $selectname); my $parser = HTML::TokeParser->new(\$html) || die $!; while (my $token = $parser->get_token) { my $type = shift @{ $token }; if ($type eq "S") { my ($tag, $attr, $attrseq, $text) = @{ $token }; if ($tag eq "form") { $formname = $attr->{'name'} || 'none'; } elsif ($tag eq "input") { push (@{$data{$formname}}, { 'type' => $attr->{'type'}, 'field_name' => $attr->{'name'} || 'none', }) if defined $formname; } elsif ($tag eq "select") { $selectname = $attr->{'name'} || 'none'; } elsif ($tag eq "option") { push (@{$data{$formname}}, { 'type' => "select", 'field_name' => $selectname, 'value' => $attr->{'value'} }) if defined $selectname; }; }; }; my ($xml) = XML::Simple->new(); print STDOUT $xml->XMLout(\%data); exit 0;
... and the sample output ...
<opt> <none field_name="node" type="text" /> <none field_name="go_button" type="image" /> <none field_name="node_id" type="hidden" /> <none field_name="vc" type="hidden" /> <none field_name="op" type="hidden" /> <none field_name="node_id" type="hidden" /> <none field_name="op" type="hidden" /> <none field_name="user" type="text" /> <none field_name="passwd" type="password" /> <none field_name="expires" type="checkbox" /> <none field_name="login" type="submit" /> <none field_name="node_id" type="hidden" /> <none field_name="node_id" type="hidden" /> <none field_name="displaytype" type="hidden" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="vote" type="radio" /> <none field_name="none" type="submit" /> </opt>
Now it should be relatively straight-forward as to how you can modify this to your needs and give you an idea of the ease with which HTML::TokeParser can extract information from HTML pages.
Update - Added support for select and option parsing
Ooohhh, Rob no beer function well without!
In reply to Re: HTML::Parser Assistance Requested
by rob_au
in thread HTML::Parser Assistance Requested
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |