1 package HTML::LinkExtor;
3 # $Id: LinkExtor.pm,v 1.33 2003/10/10 10:20:56 gisle Exp $
6 @ISA = qw(HTML::Parser);
7 $VERSION = sprintf("%d.%02d", q$Revision: 1.33 $ =~ /(\d+)\.(\d+)/);
11 HTML::LinkExtor - Extract links from an HTML document
15 require HTML::LinkExtor;
16 $p = HTML::LinkExtor->new(\&cb, "http://www.perl.org/");
18 my($tag, %links) = @_;
19 print "$tag @{[%links]}\n";
21 $p->parse_file("index.html");
25 I<HTML::LinkExtor> is an HTML parser that extracts links from an
26 HTML document. The I<HTML::LinkExtor> is a subclass of
27 I<HTML::Parser>. This means that the document should be given to the
28 parser by calling the $p->parse() or $p->parse_file() methods.
35 # legacy (some applications grabs this hash directly)
36 use vars qw(%LINK_ELEMENT);
37 *LINK_ELEMENT = \%HTML::Tagset::linkElements;
41 =item $p = HTML::LinkExtor->new
43 =item $p = HTML::LinkExtor->new( $callback )
45 =item $p = HTML::LinkExtor->new( $callback, $base )
47 The constructor takes two optional arguments. The first is a reference
48 to a callback routine. It will be called as links are found. If a
49 callback is not provided, then links are just accumulated internally
50 and can be retrieved by calling the $p->links() method.
52 The $base argument is an optional base URL used to absolutize all URLs found.
53 You need to have the I<URI> module installed if you provide $base.
55 The callback is called with the lowercase tag name as first argument,
56 and then all link attributes as separate key/value pairs. All
57 non-link attributes are removed.
63 my($class, $cb, $base) = @_;
64 my $self = $class->SUPER::new(
65 start_h => ["_start_tag", "self,tagname,attr"],
66 report_tags => [keys %HTML::Tagset::linkElements],
68 $self->{extractlink_cb} = $cb;
71 $self->{extractlink_base} = URI->new($base);
78 my($self, $tag, $attr) = @_;
80 my $base = $self->{extractlink_base};
81 my $links = $HTML::Tagset::linkElements{$tag};
82 $links = [$links] unless ref $links;
87 next unless exists $attr->{$a};
88 push(@links, $a, $base ? URI->new($attr->{$a}, $base)->abs($base)
92 $self->_found_link($tag, @links);
98 my $cb = $self->{extractlink_cb};
102 push(@{$self->{'links'}}, [@_]);
108 Returns a list of all links found in the document. The returned
109 values will be anonymous arrays with the follwing elements:
111 [$tag, $attr => $url1, $attr2 => $url2,...]
113 The $p->links method will also truncate the internal link list. This
114 means that if the method is called twice without any parsing
115 between them the second call will return an empty list.
117 Also note that $p->links will always be empty if a callback routine
118 was provided when the I<HTML::LinkExtor> was created.
125 exists($self->{'links'}) ? @{delete $self->{'links'}} : ();
128 # We override the parse_file() method so that we can clear the links
129 # before we start a new file.
133 delete $self->{'links'};
134 $self->SUPER::parse_file(@_);
141 This is an example showing how you can extract links from a document
148 $url = "http://www.perl.org/"; # for instance
149 $ua = LWP::UserAgent->new;
151 # Set up a callback that collect image links
154 my($tag, %attr) = @_;
155 return if $tag ne 'img'; # we only look closer at <img ...>
156 push(@imgs, values %attr);
159 # Make the parser. Unfortunately, we don't know the base yet
160 # (it might be diffent from $url)
161 $p = HTML::LinkExtor->new(\&callback);
163 # Request document and parse it as it arrives
164 $res = $ua->request(HTTP::Request->new(GET => $url),
165 sub {$p->parse($_[0])});
167 # Expand all image URLs to absolute ones
168 my $base = $res->base;
169 @imgs = map { $_ = url($_, $base)->abs; } @imgs;
172 print join("\n", @imgs), "\n";
176 L<HTML::Parser>, L<HTML::Tagset>, L<LWP>, L<URI::URL>
180 Copyright 1996-2001 Gisle Aas.
182 This library is free software; you can redistribute it and/or
183 modify it under the same terms as Perl itself.