2

This is my code to extract certain data under the header Item Drop%. I want to extract the 90.5% under that header. But i can only extract the whole column not just that value. any idea ?

#!/usr/bin/perl

use strict;
use warnings;

use HTML::TableExtract;
use LWP::Simple;

my $file = 'data.html';
unless ( -e $file ) {
    my $rc = getstore(
        'proj/Desktop/folder1/data.html',
        $file);
    die "Failed to download document\n" unless $rc == 200;
}



my $te = HTML::TableExtract->new( headers => qw(Item Drop%)]);

$te->parse_file($file);

my ($table) = $te->tables;

foreach my $ts (ts->tables) {
    print "Table (", join(',', $ts->coords), ");\n";
    foreach my $row ($ts->rows) {
        print join(',', @$row), "\n";
    }
}

My data.html is:


 ..
 ..
 ..
<table align = "center" class="" style= .......>
<tr>
<th rowspan="2">EM</th>
<th colspan="2"><a href= "proj/Desktop/folder1/data.html" class = ..../th>
<td> 90.5%</td>
</tr>
..
..
..
..
<tr>
<th rowspan="2">EM</th>
<th colspan="2"><a href= "proj/Desktop/folder1/data.html" class = ..../th>
<td> 40%</td>
</tr>

</table>
7
  • Try removing the attribs or headers from the constructor.
    – choroba
    Jul 13, 2021 at 9:58
  • i removed but it just extract the first table it sees Jul 14, 2021 at 2:56
  • So try to identify the table somehow, i.e. ($te->tables)[1] etc.
    – choroba
    Jul 14, 2021 at 7:56
  • @choroba hi i have updated the question Jul 15, 2021 at 8:20
  • Your script contains errors. Also, the HTML you provided doesn't demonstrate the problem you claim to have.
    – choroba
    Jul 15, 2021 at 11:24

1 Answer 1

1

My though is that is most of the time the better way for HTML scraping in any language, and not limited to tables. Perl's HTML::TreeBuilder::XPath is a must have, and easily fetch your value, check:

#!/usr/bin/env perl
use strict; use warnings;
use HTML::TreeBuilder::XPath;

my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse_file("./data.html");
print [$tree->findvalues('//table//td[contains(text(), "%")')]->[0];

Output

90.5%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .