Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: WWW::Mechanize::Chrome VERY slow on xpath obtaining TDs of a TR

by ait (Hermit)
on Nov 25, 2022 at 21:18 UTC ( [id://11148395]=note: print w/replies, xml ) Need Help??


in reply to WWW::Mechanize::Chrome VERY slow on xpath obtaining TDs of a TR

Thank you all, as always, for you valuable input and ideas! Ye monks are a smart bunch.

As much as I'd love to help debug W::M::Chrome, I have a short deadline so I decided to use LanX's idea to use xpath to get the table node and the HTML content and then parse that in Perl land. I decided to use HTML::Tree which is simple and tried.

For anyone having a similar issue, here is the code I wrote for this (assuming it has thead, th, and tbody, YMMV):

my @nodes = $mech->xpath('//table'); my @data = parse_table($nodes[0]); sub parse_table ($table_node){ my $root = HTML::TreeBuilder->new_from_content($table_node->get_at +tribute('outerHTML')); my @tparts = $root->find_by_tag_name('table')->content_list; my @colnames = ( ); my @data; foreach my $tpart (@tparts){ if($tpart->tag eq 'thead'){ my @rows = $tpart->content_list; foreach my $row (@rows) { if($row->tag eq 'tr'){ my @cells = $row->content_list; # assumes no TH is empty (see below safeguard for +data cells) foreach (@cells) { push @colnames, $_->content->[0]; } } } } elsif($tpart->tag eq 'tbody'){ my @rows = $tpart->content_list; foreach my $row (@rows) { my %row_data = (); if($row->tag eq 'tr'){ my @cells = $row->content_list; foreach (0..$#cells) { # HTML::Element's content method weirdness if($cells[$cell]->content && scalar(@{$cells[$ +cell]->content})){ $row_data{ $colnames[$cell] } = $cells[$ce +ll]->content->[0]; } else{ $row_data{ $colnames[$cell] } = ''; } } } push @data, \%row_data; } } } return \@data; }

Thanks again y'all !
--
Alex

  • Comment on Re: WWW::Mechanize::Chrome VERY slow on xpath obtaining TDs of a TR
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found