For this I would look at HTML::TokeParser for which there is an excellent tutorial on this site by crazyinsomniac - This tutorial also takes you step-by-step through the process of creating a sample application.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.