Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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(''); 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:
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?:

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.


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?

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
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-07-17 09:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.