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

How can i access only the table data in a HTML file? The HTML file contains many other data and a table with some reference data. How can i access only this table data?

Replies are listed 'Best First'.
Re: Fetching a table data in a HMTL file
by wind (Priest) on Mar 22, 2011 at 09:12 UTC

    Use LWP::Simple to download the html file, assuming you need to.

    Then use HTML::TableExtract to extract the table data. If that's insufficient, then you can also use heavier parsers like HTML::Parser.

    - Miller

      I prefer to use HTML::TreeBuilder. For example, this code will pull out the table on the PerlMonks home page that lists the currently logged in users:

      use 5.010; use warnings; use strict; use HTML::TreeBuilder; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $fetch_result = $ua->get("http://www.perlmonks.org"); my $tree = HTML::TreeBuilder->new_from_content($fetch_result->content) +; my $nodelet_table = $tree->look_down( '_tag' => 'table', 'id'=>'nodele +t_container' ); my $other_users_cell = $nodelet_table->look_down('id'=>'Other_Users'); my @other_users_list = $other_users_cell->look_down('_tag'=>'li'); print "User logged into PerlMonks:\n"; foreach my $user_element ( @other_users_list ) { print "\t".$user_element->as_text."\n"; }
        thanks chrestomanci !!! i'm feeling lucky :)
Re: Fetching a table data in a HMTL file
by Anonymous Monk on Mar 22, 2011 at 08:11 UTC
    In the search engine form type html table, like html table, and be amazed at the results :)
      Use HTML::TreeBuilder to export your HTML into a perl data structure. use Data::Dumper to figure out how that structure looks and where in the whole structure the table you desire resides. Access the Table from the above information.
Re: Fetching a table data in a HMTL file
by samarzone (Pilgrim) on Mar 22, 2011 at 10:37 UTC

    I can offer a non-perlish suggestion. Open the HTML file in IE, right click on the table and click on "Export to Microsoft Excel". I assume you are on Windows.

    --
    Regards
    - Samar
Re: Fetching a table data in a HMTL file
by Cicatrix (Novice) on Mar 23, 2011 at 04:38 UTC
    thank you all for your inputs :)