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
19       document.  The HTML::LinkExtor is a subclass of HTML::Parser. This
20       means 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
27           reference to a callback routine. It will be called as links are
28           found. If a callback is not provided, then links are just
29           accumulated internally and can be retrieved by calling the
30           $p->links() method.
31
32           The $base argument is an optional base URL used to absolutize all
33           URLs found.  You need to have the URI module installed if you
34           provide $base.
35
36           The callback is called with the lowercase tag name as first
37           argument, and then all link attributes as separate key/value pairs.
38           All non-link attributes are removed.
39
40       $p->links
41           Returns a list of all links found in the document.  The returned
42           values will be anonymous arrays with the following elements:
43
44             [$tag, $attr => $url1, $attr2 => $url2,...]
45
46           The $p->links method will also truncate the internal link list.
47           This means that if the method is called twice without any parsing
48           between them the second call will return an empty list.
49
50           Also note that $p->links will always be empty if a callback routine
51           was provided when the HTML::LinkExtor was created.
52

EXAMPLE

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

SEE ALSO

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