Skip to content

Instantly share code, notes, and snippets.

@m0veax
Created August 18, 2016 07:12
Show Gist options
  • Save m0veax/1a7d3e20f33cbc689f27a9dc35969042 to your computer and use it in GitHub Desktop.
Save m0veax/1a7d3e20f33cbc689f27a9dc35969042 to your computer and use it in GitHub Desktop.
create csv output from the first found table with tbody in a html document string
use strict;
use warnings;
use DDP;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";
use Mojo::DOM;
use Path::Tiny;
my $html = path(shift)->slurp;
my $dom = Mojo::DOM->new($html);
my $table = $dom->find("table > tbody")->first;
my $rows = $table->children("tr");
my $data_table = [];
for my $tr (@$rows) {
my $cells = $tr->children("td");
my $values = [];
for my $td ( @$cells ) {
my $text = $td->all_text;
$text =~ s/^\s+|\s+$//g;
$text =~ s/\n/\,/g;
push @$values, $text;
}
print join(";", @$values) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment