<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package HTML::LinkExtor;

require HTML::Parser;
our @ISA = qw(HTML::Parser);
our $VERSION = '3.83';

=head1 NAME

HTML::LinkExtor - Extract links from an HTML document

=head1 SYNOPSIS

 require HTML::LinkExtor;
 $p = HTML::LinkExtor-&gt;new(\&amp;cb, "http://www.perl.org/");
 sub cb {
     my($tag, %links) = @_;
     print "$tag @{[%links]}\n";
 }
 $p-&gt;parse_file("index.html");

=head1 DESCRIPTION

I&lt;HTML::LinkExtor&gt; is an HTML parser that extracts links from an
HTML document.  The I&lt;HTML::LinkExtor&gt; is a subclass of
I&lt;HTML::Parser&gt;. This means that the document should be given to the
parser by calling the $p-&gt;parse() or $p-&gt;parse_file() methods.

=cut

use strict;
use HTML::Tagset ();

# legacy (some applications grabs this hash directly)
our %LINK_ELEMENT;
*LINK_ELEMENT = \%HTML::Tagset::linkElements;

=over 4

=item $p = HTML::LinkExtor-&gt;new

=item $p = HTML::LinkExtor-&gt;new( $callback )

=item $p = HTML::LinkExtor-&gt;new( $callback, $base )

The constructor takes two optional arguments. The first is a reference
to a callback routine. It will be called as links are found. If a
callback is not provided, then links are just accumulated internally
and can be retrieved by calling the $p-&gt;links() method.

The $base argument is an optional base URL used to absolutize all URLs found.
You need to have the I&lt;URI&gt; module installed if you provide $base.

The callback is called with the lowercase tag name as first argument,
and then all link attributes as separate key/value pairs.  All
non-link attributes are removed.

=cut

sub new
{
    my($class, $cb, $base) = @_;
    my $self = $class-&gt;SUPER::new(
                    start_h =&gt; ["_start_tag", "self,tagname,attr"],
		    report_tags =&gt; [keys %HTML::Tagset::linkElements],
	       );
    $self-&gt;{extractlink_cb} = $cb;
    if ($base) {
	require URI;
	$self-&gt;{extractlink_base} = URI-&gt;new($base);
    }
    $self;
}

sub _start_tag
{
    my($self, $tag, $attr) = @_;

    my $base = $self-&gt;{extractlink_base};
    my $links = $HTML::Tagset::linkElements{$tag};
    $links = [$links] unless ref $links;

    my @links;
    my $a;
    for $a (@$links) {
	next unless exists $attr-&gt;{$a};
	(my $link = $attr-&gt;{$a}) =~ s/^\s+//; $link =~ s/\s+$//; # HTML5
	push(@links, $a, $base ? URI-&gt;new($link, $base)-&gt;abs($base) : $link);
    }
    return unless @links;
    $self-&gt;_found_link($tag, @links);
}

sub _found_link
{
    my $self = shift;
    my $cb = $self-&gt;{extractlink_cb};
    if ($cb) {
	&amp;$cb(@_);
    } else {
	push(@{$self-&gt;{'links'}}, [@_]);
    }
}

=item $p-&gt;links

Returns a list of all links found in the document.  The returned
values will be anonymous arrays with the following elements:

  [$tag, $attr =&gt; $url1, $attr2 =&gt; $url2,...]

The $p-&gt;links method will also truncate the internal link list.  This
means that if the method is called twice without any parsing
between them the second call will return an empty list.

Also note that $p-&gt;links will always be empty if a callback routine
was provided when the I&lt;HTML::LinkExtor&gt; was created.

=cut

sub links
{
    my $self = shift;
    exists($self-&gt;{'links'}) ? @{delete $self-&gt;{'links'}} : ();
}

# We override the parse_file() method so that we can clear the links
# before we start a new file.
sub parse_file
{
    my $self = shift;
    delete $self-&gt;{'links'};
    $self-&gt;SUPER::parse_file(@_);
}

=back

=head1 EXAMPLE

This is an example showing how you can extract links from a document
received using LWP:

  use LWP::UserAgent;
  use HTML::LinkExtor;
  use URI::URL;

  $url = "http://www.perl.org/";  # for instance
  $ua = LWP::UserAgent-&gt;new;

  # Set up a callback that collect image links
  my @imgs = ();
  sub callback {
     my($tag, %attr) = @_;
     return if $tag ne 'img';  # we only look closer at &lt;img ...&gt;
     push(@imgs, values %attr);
  }

  # Make the parser.  Unfortunately, we don't know the base yet
  # (it might be different from $url)
  $p = HTML::LinkExtor-&gt;new(\&amp;callback);

  # Request document and parse it as it arrives
  $res = $ua-&gt;request(HTTP::Request-&gt;new(GET =&gt; $url),
                      sub {$p-&gt;parse($_[0])});

  # Expand all image URLs to absolute ones
  my $base = $res-&gt;base;
  @imgs = map { $_ = url($_, $base)-&gt;abs; } @imgs;

  # Print them out
  print join("\n", @imgs), "\n";

=head1 SEE ALSO

L&lt;HTML::Parser&gt;, L&lt;HTML::Tagset&gt;, L&lt;LWP&gt;, L&lt;URI::URL&gt;

=head1 COPYRIGHT

Copyright 1996-2001 Gisle Aas.

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut

1;
</pre></body></html>