I am working with a parsing an HTML table in Perl using HTML::Query. I want to go through each row of the table and look at a certain cell. However, when I use this query…
my @resultsrows = $query->query('table#player_matches.tabelle_grafik tr')->get_elements();
…I run into a problem where each row of this outermost table is not represented alone. What I mean is, while the first element of this array is the first row in the outermost table I am working with, the second one refers to another table row within that first row because one of the cells has a completely new table within it.
For further explanation; when I try to loop through it:
for(my $i = 1; $i < @resultsrows; $i++) {
@currentrow = $query->query('td span'); ## The cell I am looking for is the only one with a span.
if($currentrow[0]->attr('title') eq $searchterm) {
$returnedIndex = $i;
}
}
Is there any CSS selector that I can use in this query that will allow me to select only the outermost table and avoid this problem? Would HTML::Tree help with this?
EDIT: I have tried to use > to denote only direct children, but it is not gathering any rows then: my @resultsrows = $query->query('table#player_matches.tabelle_grafik > tr')->get_elements();
Perhaps you’re working with a DOM that collects all
trchildren of atablewithin atbody(which is what browsers usually do).Does this work?