-
Notifications
You must be signed in to change notification settings - Fork 2
/
extractlinks.pl
executable file
·158 lines (112 loc) · 3.55 KB
/
extractlinks.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl -w
##
## Extracts links from a HTML page
## Copyright (c) 2005 by Berislav Kovacki (beca/AT/sezampro.yu)
## Copyright (c) 2005,2006 by Michal Nazarewicz (mina86/AT/mina86.com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
#
# Documentation at the end of file.
#
use strict;
use warnings;
use English;
use Getopt::Long;
use Pod::Usage;
use URI::URL;
use URI::file;
use LWP::UserAgent;
use HTML::LinkExtor;
my $VERSION = '1.07';
my $url = '';
my @tags = ();
my $noabs = 0;
my $base_uri;
pod2usage() unless GetOptions(
'tag|tags|t=s' => \@tags,
'relative|r' => \$noabs,
'base|b=s' => \$base_uri,
'help|h|?' => sub { pod2usage(-verbose => 1); });
@tags = split(/,/, join ',', @tags);
my $error = 0;
my $parser = HTML::LinkExtor->new(\&linkcallback);
my $ua = LWP::UserAgent->new;
my @links;
@ARGV = ('-') unless (@ARGV);
while (@ARGV) {
$url = shift(@ARGV);
@links = ();
my $base;
if ($url eq '-') {
$parser->parse( sub { <STDIN> } );
$base = defined $base_uri ? $base_uri : URI::file->cwd;
@links = map { $_ = url($_, $base)->abs; } @links unless ($noabs);
} else {
$url = URI->new($url)->abs(URI::file->cwd);
my $res = $ua->request(HTTP::Request->new(GET => $url),
sub { $parser->parse($_[0]) });
if (!$res->is_success) {
print('Parse failed: ');
print($res->status_line);
print("\n");
$error = 1;
} elsif (!$noabs) {
$base = defined $base_uri ? $base_uri : $res->base;
@links = map { $_ = url($_, $base)->abs; } @links;
}
}
print(join("\n", @links), "\n") if (@links);
}
exit($error);
sub linkcallback {
my ($tag, %links) = @_;
push @links, values %links if (!@tags || grep { $_ eq $tag } @tags);
}
__END__
=head1 NAME
extractlinks - Extracts links from a HTML page
=head1 DESCRIPTION
The extractlinks utility shall search the HTML file specified by the
url parameter and extract all contained links. The url must be
specified as absolute URL.
=head1 SYNOPSIS
extractlinks [ -t tags ] [ -r | -b base ] [ -- ] [ url ... ]
=head1 OPTIONS
=over 8
=item B<-h --help>
Displays help screen.
=item B<-r --relative>
Won't make links absolute.
=item B<-b --base=>I<base>
Uses I<base> as relative links base address instead of base address of
input file.
=item B<-t --tag=>I<tags>
A comma separated list of HTML tags to search for links. This option
may be specified several times. If not give, links from all HTML rags
are extracted.
=item I<url>
Can be a URI (eg. I<http://mina86.com/>), a file name
(eg. I<index.html>) or a minus sign which will cause the script to
read HTML page from standard input. If no URLs are given, - is
assumed. Beware that I<www.google.com> will cause script to open a
B<file> of a given name and not a Google page.
=back
=head1 AUTHOR
Berislav Kovacki <[email protected]>,
Michal Nazarewicz <[email protected]>
=cut