1HTML::LinkExtor(3)    User Contributed Perl Documentation   HTML::LinkExtor(3)
2
3
4

NAME

6       HTML::LinkExtor - Extract links from an HTML document
7

SYNOPSIS

9        require HTML::LinkExtor;
10        $p = HTML::LinkExtor->new(\&cb, "http://www.perl.org/");
11        sub cb {
12            my($tag, %links) = @_;
13            print "$tag @{[%links]}\n";
14        }
15        $p->parse_file("index.html");
16

DESCRIPTION

18       HTML::LinkExtor is an HTML parser that extracts links from an HTML doc‐
19       ument.  The HTML::LinkExtor is a subclass of HTML::Parser. This means
20       that the document should be given to the parser by calling the
21       $p->parse() or $p->parse_file() methods.
22
23       $p = HTML::LinkExtor->new
24       $p = HTML::LinkExtor->new( $callback )
25       $p = HTML::LinkExtor->new( $callback, $base )
26           The constructor takes two optional arguments. The first is a refer‐
27           ence to a callback routine. It will be called as links are found.
28           If a callback is not provided, then links are just accumulated
29           internally and can be retrieved by calling the $p->links() method.
30
31           The $base argument is an optional base URL used to absolutize all
32           URLs found.  You need to have the URI module installed if you pro‐
33           vide $base.
34
35           The callback is called with the lowercase tag name as first argu‐
36           ment, and then all link attributes as separate key/value pairs.
37           All non-link attributes are removed.
38
39       $p->links
40           Returns a list of all links found in the document.  The returned
41           values will be anonymous arrays with the follwing elements:
42
43             [$tag, $attr => $url1, $attr2 => $url2,...]
44
45           The $p->links method will also truncate the internal link list.
46           This means that if the method is called twice without any parsing
47           between them the second call will return an empty list.
48
49           Also note that $p->links will always be empty if a callback routine
50           was provided when the HTML::LinkExtor was created.
51

EXAMPLE

53       This is an example showing how you can extract links from a document
54       received using LWP:
55
56         use LWP::UserAgent;
57         use HTML::LinkExtor;
58         use URI::URL;
59
60         $url = "http://www.perl.org/";  # for instance
61         $ua = LWP::UserAgent->new;
62
63         # Set up a callback that collect image links
64         my @imgs = ();
65         sub callback {
66            my($tag, %attr) = @_;
67            return if $tag ne 'img';  # we only look closer at <img ...>
68            push(@imgs, values %attr);
69         }
70
71         # Make the parser.  Unfortunately, we don't know the base yet
72         # (it might be diffent from $url)
73         $p = HTML::LinkExtor->new(\&callback);
74
75         # Request document and parse it as it arrives
76         $res = $ua->request(HTTP::Request->new(GET => $url),
77                             sub {$p->parse($_[0])});
78
79         # Expand all image URLs to absolute ones
80         my $base = $res->base;
81         @imgs = map { $_ = url($_, $base)->abs; } @imgs;
82
83         # Print them out
84         print join("\n", @imgs), "\n";
85

SEE ALSO

87       HTML::Parser, HTML::Tagset, LWP, URI::URL
88
90       Copyright 1996-2001 Gisle Aas.
91
92       This library is free software; you can redistribute it and/or modify it
93       under the same terms as Perl itself.
94
95
96
97perl v5.8.8                       2006-04-26                HTML::LinkExtor(3)
Impressum