Last active
September 10, 2016 07:44
-
-
Save ggl/9ef9365c790e6a13e215 to your computer and use it in GitHub Desktop.
Get href links from an URL using Mojo::UserAgent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use Mojo::URL; | |
use Mojo::UserAgent; | |
my $ua = Mojo::UserAgent->new->max_redirects(3); | |
$ua->transactor->name('Mozilla/5.0 (Linux; Android 4.4; Nexus 5)'); | |
my $url = Mojo::URL->new($ARGV[0] || 'http://localhost'); | |
my $tx = $ua->get($url); | |
if (my $res = $tx->success) { | |
my $links = $res->dom->find('a')->map(attr => 'href')->to_array; | |
print Dumper $links; | |
} | |
else { | |
my $err = $tx->error; | |
die "$err->{code} response: $err->{message}" if $err->{'code'}; | |
die "Connection error: $err->{message}"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment