When you wish to parse a HTML document (or a look alike) using HTML::TokeParser, and you're not quite sure how the document will be tokenized, this snippet can give you a good idea of what's going on.

To run, simply type "perl scriptname.pl url" you'll a dump on STDOUT looking like:

TYPE : D #### 0:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > #### 1: #### #################################################### TYPE : T #### 0: #### 1: #### 2: #### #################################################### TYPE : C #### 0:<!--took this out for IE6ites "http://www.w3.org/TR/REC-html40/loos +e.dtd"--> #### 1: #### #################################################### TYPE : T #### 0: #### 1: #### 2: #### #################################################### TYPE : S #### 0:html #### 1:HASH(0x1afe3f4) #### 2:ARRAY(0x1afe40c) #### 3:<HTML> #### 4: #### ####################################################
#!/usr/bin/perl -w use strict; use LWP::Simple; use HTML::TokeParser; my $url = shift or die "usage: ". __FILE__ ." url"; my $rawHTML = get($url); # attempt to d/l the page to mem die "LWP::Simple messed up $!" unless ($rawHTML); my $tp; $tp = HTML::TokeParser->new(\$rawHTML) or die "WTF $tp gone bad: $!"; # And now -- a generic HTML::TokeParser loop while (my $token = $tp->get_token) { my $ttype = shift @{ $token }; print "TYPE : $ttype\n####\n"; printf( join( '', map { "$_:%s\n####\n" } 0..@{$token} ) , @{$token} ); print "####################################################\n\n"; }

In reply to HTML::TokeParser token dumper by crazyinsomniac

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.