in reply to Re: HTML::TableExtract issues
in thread HTML::TableExtract issues

Hi choroba

Thank you for your reply.

my @values = map defined($_) ? $_ : ' ', @$row;

works perfectly. I see the problem with using grep in this case.

Changing: use HTML::TableExtract;

to: use HTML::TableExtract qw (tree);

yields: HTML::ElementTable::DataElement=HASH(0x3275f84), which repeats with different letters and numbers in between the HASH(), which I assume is the data within each cell of each table. More Googling required!!!

Cheers

Mr Bigglesworth

Replies are listed 'Best First'.
Re^3: HTML::TableExtract issues
by poj (Abbot) on Aug 24, 2013 at 16:17 UTC
    "What I would like to do is populate the first cell in each row with the 58035.png (in this case)."

    One way is by using the tree mode and look_down like this.

    #!perl use strict; use warnings; use HTML::TableExtract 'tree'; use Text::CSV; use LWP::Simple; # input my $html = get('your url'); my $te = HTML::TableExtract->new(); $te->parse($html); # output my $csvfile = 'results.csv'; my $csv = Text::CSV->new ( { binary => 1, eol => "\n" } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, '>:encoding(utf-8)', $csvfile or die "$csvfile : $!"; # process my $count=0; printf "%3s %4s %4s\n",'Tbl','Rows','Cols'; foreach my $ts ($te->tables){ my $tree = $ts->tree(); printf "%3d %4d %4d\n",++$count,$tree->maxrow,$tree->maxcol; foreach my $r (0..$tree->maxrow){ my @cells=(); # is col 1 an img ? my $x = $tree->cell($r,0)->look_down('src',qr/png$/); push @cells,(defined $x) ? $x->attr('src') : $tree->cell($r,0)->as +_text; for my $c (1..$tree->maxcol){ my $val = $tree->cell($r,$c)->as_text; push @cells,$val; } $csv->print ($fh, \@cells); } } close $fh or die "$csvfile: $!";

    Notice I have used Text::CSV rather than just adding commas between columns.

    poj

      Hi poj

      Thank you for your efforts, it is appreciated. It works great.

      I need to change it a little so that I can process the text inside the resulting CSV a little, but you have push me ahead quite a bit.

      I did notice the use of Text::CSV, that is definitely a much better way to do it, certainly a much more reliable way

      Cheers

      Mr Bigglesworth

Re^3: HTML::TableExtract issues
by Laurent_R (Canon) on Aug 24, 2013 at 15:29 UTC

    The documentation for HTML::TableExtract gives you an example using the tree option:

    # Example: Extracting as an HTML::Element tree structure # Rather than extracting raw text, the html can be converted into a # tree of element objects. The HTML document is composed of # HTML::Element objects and the tables are HTML::ElementTable # structures. Using this, the contents of tables within a document ca +n # be edited in-place. use HTML::TableExtract qw(tree); $te = HTML::TableExtract->new( headers => qw(Fee Fie Foe Fum) ); $te->parse_file($html_file); $table = $te->first_table_found; $table_tree = $table->tree; $table_tree->cell(4,4)->replace_content('Golden Goose'); $table_html = $table_tree->as_HTML; $table_text = $table_tree->as_text; $document_tree = $te->tree; $document_html = $document_tree->as_HTML;

      Hi Laurent_R

      I have been trying to get my head around the documentation for a while (about a week) now but have been really struggling with it, but starting to make some progress.

      Thank you.

      Mr Bigglesworth