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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.