yusy has asked for the wisdom of the Perl Monks concerning the following question:

I used html::tablecontentparser to extract tables from http://www.municipalcourt.org:81/connection/court/lookup.xsp?in=ct&case=04TRD02044-A&view=detail and write all information to an excel file. But, the code and the result as following: please help me

use LWP::Simple; use URI; use HTML::TableContentParser; use HTML::Entities; our $page; our $tables; our $tcp = new HTML::TableContentParser; our $modules; our $query; our $count=0; our $query = "http://www.municipalcourt.org:81/connection/court/lookup +.xsp?in=ct&case=04TRD02044-A&view=detail"; $page = get($query); $p = HTML::TableContentParser->new(); $tables = $p->parse($page); for $t (@$tables) { for $r (@{$t->{rows}}) { print "Row: "; for $c (@{$r->{cells}}) { print "[$c->{data}] "; } print "\n"; } }
results:
Row: Row: [ ] Row: Row: [<a href="http://www.municipalcourt.org" class="style1">Home</a>] Row: Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=summary"> +-Summary</ a>] Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=status">- +Status</a> ] Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=docket">- +Docket</a> ] Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=balance"> +-Balance</ a>] Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=payments" +>-Payments </a>] Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=priors">- +Priors</a> ] Row: [<a href="lookup.xsp?in=ct&amp;case=04TRD02044&amp;view=aka">-AKA +</a>] Row: Row: [<a href="lookup.xsp?in=cv">Civil&#32893;Lookup</a>] Row: [<a href="lookup.xsp?in=ct">TR/CR&#32893;Lookup</a>] Row: Row: [<a href="query.xsp?in=cv&amp;option=index">Civil&#32893;Query</a +>] Row: [<a href="query.xsp?in=ct&amp;option=index">TR/CR&#32893;Query</a +>] Row: [] [ <h2>Detail for TR/CR Case <em>04TRD02044-A</em> </h2> ] Row: [] Row: [ ] Row: Row: Row: .....................................

Replies are listed 'Best First'.
Re: need help to extract table form html
by shmem (Chancellor) on Dec 15, 2007 at 13:45 UTC
    Correct. Your code produces that output. But what did you expect? See I know what I mean. Why don't you?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: need help to extract table form html
by whakka (Hermit) on Dec 16, 2007 at 07:58 UTC
    I'm reasonably assuming you wanted all of the data in the tables to print to Excel. May I recommend HTML::TreeBuilder instead because of its wonderful "look_down" function, which I have found to be more intuitive to learn and use than HTML::TableContentParser. It matches the tags with the parameters you supply it, and returns all of its children.

    For example:

    use HTML::TreeBuilder; #Parse html content using html-treebuilder: my $root = HTML::TreeBuilder->new(); $root->parse($html); #from LWP content() $root->eof(); my @tables = $root->look_down(_tag => 'table'); while (@tables) { my $node = shift @tables; if (ref $node) { unshift @tables, $node->content_list; } else { print $node."\n"; } } $root = $root->delete;
    This simple example will output much more readable text with all of the relevant information contained within. For a good tutorial see O'Reilly's book on the subject. Also see here for a great tutorial about thinking of HTML as a tree structure.