Mojo :: DOM shortcut to get absolute resource url?

When analyzing a web page with Mojo::DOM(or any other structure) it is quite common to pull out the address of a resource, which can be either relative or absolute. Is there a shortcut method for translating such a resource address to an absolute URL?

The following command mojopulls all stylesheets to mojolicio.us:

$ mojo get http://mojolicio.us "link[rel=stylesheet]" attr href
/mojo/prettify/prettify-mojo-light.css
/css/index.css

And the following script does the same, but also uses URIit to translate the resource into an absolute URL.

use strict;
use warnings;

use Mojo::UserAgent;
use URI;

my $url = 'http://mojolicio.us';

my $ua = Mojo::UserAgent->new;
my $dom = $ua->get($url)->res->dom;

for my $csshref ($dom->find('link[rel=stylesheet]')->attr('href')->each) {
    my $cssurl = URI->new($csshref)->abs($url);
    print "$cssurl\n";
}

Outputs:

http://mojolicio.us/mojo/prettify/prettify-mojo-light.css
http://mojolicio.us/css/index.css

Obviously, the relative URL in this context should be absolute using the URL that the DOM was loading. However, I do not know how to get the absolute URL of a resource other than the encoding itself.

Mojolicious Mojo::URL #to_abs. , , - Mojo::DOM , URI.

, - script, , - Mojo :

mojo get http://mojolicio.us "link[rel=stylesheet]" attr href to_abs
+4
1

, , Mojo::URL ? URL- (, , ), $base.

, $base Mojo::URL, $base->new. , , Mojo::URL->new.

use Mojo::Base -strict;
use Mojo::UserAgent;

my $url = 'http://mojolicio.us';

my $ua = Mojo::UserAgent->new->max_redirects(10);
my $tx = $ua->get($url);
my $base = $tx->req->url;

$tx->res
  ->dom
  ->find('link[rel=stylesheet]')
  ->map(sub{$base->new($_->{href})->to_abs($base)})
  ->each(sub{say});
+1

Source: https://habr.com/ru/post/1543503/


All Articles