use HTML::Parser (); my $CHUNK = 2; # get Nth instance of text12 $p = HTML::Parser->new( api_version => 3, start_h => [ \&start, "self, tagname, attr" ], end_h => [ \&end, "self, tagname" ], text_h => [ \&text, "self, dtext" ] ); $p->parse_file(*DATA); sub start{ my ( $self, $tagname, $attr ) = @_; if ( $tagname eq 'div' and $attr->{class} eq 'text12' ) { $self->{text12}++; $self->{text12_item}++; } } sub end{ my ( $self, $tagname ) = @_; $self->{text12} = 0 if $tagname eq 'div'; } sub text{ my ( $self, $text ) = @_; print $text if $self->{text12} and $self->{text12_item} == $CHUNK; } __DATA__
Chunk 1
Chunk 2
Chunk 3