igoryonya has asked for the wisdom of the Perl Monks concerning the following question:
Here is the test code:
It works, but, is there a way to list the existing attributes from a given tag?#!/usr/bin/env perl use utf8; use feature 'say'; use Mojo::UserAgent; my $data = undef; #Get correct file my $cache = shift // '/tmp/cache.html'; if(-d $cache){ die("'$cache' is folder. You need to use file."); } if(-e $cache){ $data = Mojo::File->new($cache)->slurp; }else{ say 'Fetching fresh HTML'; my $ua = Mojo::UserAgent->new; my $tx = $ua->get('https://www.mojolicious.org'); die "Could not fetch! Error is ", $tx->result->code unless($tx->re +sult->is_success); $tx->result->content->asset->move_to($cache); $data = $tx->result->body; #->body is not only <body>...</body> +, but a complete loaded resource content for 1 file. } say "Tags are:\n\t", Mojo::DOM->new($data)->find('a')->map(attr=>'href +')->uniq->sort->join("\n\t");
Something like this, probably?:
I tried to enumerate with:Mojo::DOM->new($data)->find('a')->map(attr=>'*')->join("\n\t");
But I only got empty hash.my %attrs = "Tags are:\n\t", Mojo::DOM->new($data)->attr; map{ print "(%s)/(%s)\n", $_, $attrs{$_} } sort keys %attrs;
my $dom = Mojo::DOM->new($data); map{ say } $dom->find('a')->first; my $attrs = $dom->find('a')->first->attr; map{ printf "(%s)/(%s)\n", $_, $attrs->{$_} } sort keys %$attrs;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get a list attributes from a given tag in Mojo::DOM
by Corion (Patriarch) on Jun 13, 2024 at 10:37 UTC | |
by igoryonya (Pilgrim) on Jun 13, 2024 at 10:50 UTC | |
by Corion (Patriarch) on Jun 13, 2024 at 10:54 UTC |