home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / HTML / LinkExtor.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  4.1 KB  |  169 lines

  1. package HTML::LinkExtor;
  2.  
  3. =head1 NAME
  4.  
  5. HTML::LinkExtor - Extract links from an HTML document
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.  require HTML::LinkExtor;
  10.  $p = HTML::LinkExtor->new(\&cb, "http://www.sn.no/");
  11.  sub cb {
  12.      my($tag, %links) = @_;
  13.      print "$tag @{[%links]}\n";
  14.  }
  15.  $p->parse_file("index.html");
  16.  
  17. =head1 DESCRIPTION
  18.  
  19. The I<HTML::LinkExtor> (link extractor) is an HTML parser that takes a
  20. callback routine as parameter.  This routine is then called as the
  21. various link attributes are recognized.
  22.  
  23. The I<HTML::LinkExtor> is a subclass of I<HTML::Parser>. This means
  24. that the document should be given to the parser by calling the
  25. $p->parse() or $p->parse_file() methods.
  26.  
  27. =cut
  28.  
  29. require HTML::Parser;
  30. @ISA = qw(HTML::Parser);
  31.  
  32. use URI::URL qw(url);
  33.  
  34. use strict;
  35. use vars qw(%LINK_ELEMENT);
  36.  
  37. %LINK_ELEMENT =
  38. (
  39.  body   => 'background',
  40.  base   => 'href',
  41.  a      => 'href',
  42.  img    => [qw(src lowsrc usemap)],   # 'lowsrc' is a Netscape invention
  43.  form   => 'action',
  44.  input  => 'src',
  45. 'link'  => 'href',          # need quoting since link is a perl builtin
  46.  frame  => 'src',
  47.  applet => 'codebase',
  48.  area   => 'href',
  49.  frame  => 'src',   # Netscape 2.0 extention
  50.  embed  => 'src',   # used in Netscape 2.0 for Shockwave and things like that
  51. );
  52.  
  53. =head2 $p = HTML::LinkExtor->new([$callback[, $base]])
  54.  
  55. The constructor takes two optional argument. The first is a reference
  56. to a callback routine. It will be called as links are found. If a
  57. callback is not provided, then links are just accumulated internally
  58. and can be retrieved by calling the $p->links() method. The $base is
  59. an optional base URL used to absolutize all URLs found.
  60.  
  61. The callback is called with the lowercase tag name as first argument,
  62. and then all link attributes as separate key/value pairs.  All
  63. non-link attributes are removed.
  64.  
  65. =cut
  66.  
  67. sub new
  68. {
  69.     my($class, $cb, $base) = @_;
  70.     my $self = $class->SUPER::new;
  71.     $self->{extractlink_cb} = $cb;
  72.     $self->{extractlink_base} = $base;
  73.     $self;
  74. }
  75.  
  76. sub start
  77. {
  78.     my($self, $tag, $attr) = @_;  # $attr is reference to a HASH
  79.     return unless exists $LINK_ELEMENT{$tag};
  80.  
  81.     my $base = $self->{extractlink_base};
  82.     my $links = $LINK_ELEMENT{$tag};
  83.     $links = [$links] unless ref $links;
  84.  
  85.     my @links;
  86.     my $a;
  87.     for $a (@$links) {
  88.     next unless exists $attr->{$a};
  89.     push(@links, $a, $base ? url($attr->{$a}, $base)->abs : $attr->{$a});
  90.     }
  91.     return unless @links;
  92.  
  93.     my $cb = $self->{extractlink_cb};
  94.     if ($cb) {
  95.     &$cb($tag, @links);
  96.     } else {
  97.     push(@{$self->{'links'}}, [$tag, @links]);
  98.     }
  99. }
  100.  
  101. =head2 $p->links
  102.  
  103. Returns a list of all links found in the document.  The returned
  104. values will be anonymous arrays with the follwing elements:
  105.  
  106.   [$tag, $attr => $url1, $attr2 => $url2,...]
  107.  
  108. The $p->links method will also truncate the internal link list.  This
  109. means that if the method is called twice without any parsing in
  110. between then the second call will return an empty list.
  111.  
  112. Also note that $p->links will always be empty if a callback routine
  113. was provided when the L<HTML::LinkExtor> was created.
  114.  
  115. =cut
  116.  
  117. sub links
  118. {
  119.     my $self = shift;
  120.     exists($self->{'links'}) ? @{delete $self->{'links'}} : ();
  121. }
  122.  
  123. sub parse_file
  124. {
  125.     my $self = shift;
  126.     delete $self->{'links'};
  127.     $self->SUPER::parse_file(@_);
  128. }
  129.  
  130. =head1 EXAMPLE
  131.  
  132. This is an example showing how you can extract links as a document
  133. is received using LWP:
  134.  
  135.   use LWP::UserAgent;
  136.   use HTML::LinkExtor;
  137.   use URI::URL;
  138.  
  139.   $url = "http://www.sn.no/";  # for instance
  140.   $ua = new LWP::UserAgent;
  141.  
  142.   my @imgs = ();
  143.   sub callback {
  144.      my($tag, %attr) = @_;
  145.      return if $tag ne 'img';  # we only look closer at <img ...>
  146.      push(@imgs, values %attr);
  147.   }
  148.  
  149.   $p = HTML::LinkExtor->new(\&callback);
  150.  
  151.   $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])});
  152.  
  153.   my $base = $res->base;
  154.   @imgs = map { $_ = url($_, $base)->abs; } @imgs;
  155.  
  156.   print join("\n", @imgs), "\n";
  157.  
  158. =head1 SEE ALSO
  159.  
  160. L<HTML::Parser>
  161.  
  162. =head1 AUTHOR
  163.  
  164. Gisle Aas E<lt>aas@sn.no>
  165.  
  166. =cut
  167.  
  168. 1;
  169.