Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a tool to query elements out of a single page.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env perl | ||
use strict; | ||
use warnings; | ||
use encoding 'utf8'; | ||
use LWP::UserAgent; | ||
use Mojo::DOM; | ||
|
||
my ($url, $selector) = @ARGV; | ||
die "Usage: $0 url css-selecotr" unless $url && $selector; | ||
|
||
my $ua = LWP::UserAgent->new; | ||
my $response = $ua->get($url); | ||
|
||
die "Failed to get the URL" unless $response->is_success; | ||
die "The response is not HTML" unless $response->header("Content-Type") =~ m{^text/html\s*(;|$)}; | ||
|
||
my $dom = Mojo::DOM->new; | ||
$dom->parse($response->content); | ||
|
||
$dom->find($selector)->each( | ||
sub { | ||
my $el = shift; | ||
my $attrs = $el->attrs; | ||
print $el->type . ":\n"; | ||
for (keys %$attrs) { | ||
print " " . $_ . ": " . $attrs->{$_} . "\n"; | ||
} | ||
print " # " . $el->text; | ||
|
||
print "\n\n"; | ||
}); | ||
|
||
|