This uses HTML::TokeParser
#!/bin/perl5 use strict; use warnings; use HTML::TokeParser; my $file = 'map2004.html'; my $tp = HTML::TokeParser->new($file) or die "Couldn't read html file: $!"; # start tag, attrib, value my ($s_tag, $s_attrb, $s_value) = qw(div class menu); # end tag my ($e_tag) = 'h6'; my $max = 20; my $count; my $start; # flag # typo fixed my %data; # hash to hold output while ( my $tag = $tp->get_token ) { next if $tag->[0] eq 'S' and $tag->[1] eq $s_tag and exists $tag->[2]->{$s_attrb} and $tag->[2]->{$s_attrb} eq $s_value and ++$start; next unless $start; last if $tag->[0] eq 'S' and $tag->[1] eq $e_tag; if ( $tag->[0] eq 'S' and $tag->[1] eq 'a' and exists $tag->[2]->{href} ){ my $href = $tag->[2]->{href}; my $link_text = $tp->get_trimmed_text('/a'); $data{$href} = $link_text; $count++; last if $count == $max; } } for my $key (sort keys %data){ print "$key -> $data{$key}\n"; } # ["S", $tag, $attr, $attrseq, $text] # ["E", $tag, $text] # ["T", $text, $is_data] # ["C", $text] # ["D", $text] # ["PI", $token0, $text]

update

"...specify in some simple way what data to extract..."
That's trickier because it depends on 'what data'.
I've always found it relatively easy to adapt the above type of script.

update 2

Fixed typo.


In reply to Re: HTML::Parser API script or module by wfsp
in thread HTML::Parser API script or module by ldln

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.