Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

SOLVED: Get a list attributes from a given tag in Mojo::DOM

by igoryonya (Pilgrim)
on Jun 13, 2024 at 10:31 UTC ( [id://11159945]=perlquestion: print w/replies, xml ) Need Help??

igoryonya has asked for the wisdom of the Perl Monks concerning the following question:

I am learning a Mojo::UserAgent module and I've stumbled upon an issue, that I can't find an answer to on the internet and Mojo's mans.

Here is the test code:

#!/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");
It works, but, is there a way to list the existing attributes from a given tag?
I tried to find it out from Mojo:
:DOM
:Asset
:Content
and I didn't find any mentioning of attr enumeration or conversion to string.
Is there a way to find out what tags exist on found tag?

Something like this, probably?:

Mojo::DOM->new($data)->find('a')->map(attr=>'*')->join("\n\t");
I tried to enumerate with:
my %attrs = "Tags are:\n\t", Mojo::DOM->new($data)->attr; map{ print "(%s)/(%s)\n", $_, $attrs{$_} } sort keys %attrs;
But I only got empty hash.

UPDATE

OK, i've figured it out:
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

    See Mojo::DOM->attr.

    my $hash = $dom->attr; say $_ for keys $hash->%*; # for Perl since 5.36 say $_ for keys %$hash; # for all versions of Perl
      Ye, I got rusty. Forgot about hashref :)
      Is $dom supposed to be?:
      my $dom = Mojo::DOM->new($data)
      from my example?

        The "main" DOM for the whole document is my $dom = Mojo::DOM->new($data);. Then you likely want to ->find() some element in it, which again is a Mojo::DOM object. I'm sure the linked documentation tells you more about the usage.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11159945]
Approved by Corion
Front-paged by cavac
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2025-06-17 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.