{
    "mode": "perldoc",
    "parameter": "HTML::LinkExtor",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/HTML%3A%3ALinkExtor/json",
    "generated": "2026-06-12T06:20:53Z",
    "synopsis": "require HTML::LinkExtor;\n$p = HTML::LinkExtor->new(\\&cb, \"http://www.perl.org/\");\nsub cb {\nmy($tag, %links) = @;\nprint \"$tag @{[%links]}\\n\";\n}\n$p->parsefile(\"index.html\");",
    "sections": {
        "NAME": {
            "content": "HTML::LinkExtor - Extract links from an HTML document\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "require HTML::LinkExtor;\n$p = HTML::LinkExtor->new(\\&cb, \"http://www.perl.org/\");\nsub cb {\nmy($tag, %links) = @;\nprint \"$tag @{[%links]}\\n\";\n}\n$p->parsefile(\"index.html\");\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "*HTML::LinkExtor* is an HTML parser that extracts links from an HTML document. The\n*HTML::LinkExtor* is a subclass of *HTML::Parser*. This means that the document should be given\nto the parser by calling the $p->parse() or $p->parsefile() methods.\n\n$p = HTML::LinkExtor->new\n$p = HTML::LinkExtor->new( $callback )\n$p = HTML::LinkExtor->new( $callback, $base )\nThe constructor takes two optional arguments. The first is a reference to a callback\nroutine. It will be called as links are found. If a callback is not provided, then links are\njust accumulated internally and can be retrieved by calling the $p->links() method.\n\nThe $base argument is an optional base URL used to absolutize all URLs found. You need to\nhave the *URI* module installed if you provide $base.\n\nThe callback is called with the lowercase tag name as first argument, and then all link\nattributes as separate key/value pairs. All non-link attributes are removed.\n\n$p->links\nReturns a list of all links found in the document. The returned values will be anonymous\narrays with the following elements:\n\n[$tag, $attr => $url1, $attr2 => $url2,...]\n\nThe $p->links method will also truncate the internal link list. This means that if the\nmethod is called twice without any parsing between them the second call will return an empty\nlist.\n\nAlso note that $p->links will always be empty if a callback routine was provided when the\n*HTML::LinkExtor* was created.\n",
            "subsections": []
        },
        "EXAMPLE": {
            "content": "This is an example showing how you can extract links from a document received using LWP:\n\nuse LWP::UserAgent;\nuse HTML::LinkExtor;\nuse URI::URL;\n\n$url = \"http://www.perl.org/\";  # for instance\n$ua = LWP::UserAgent->new;\n\n# Set up a callback that collect image links\nmy @imgs = ();\nsub callback {\nmy($tag, %attr) = @;\nreturn if $tag ne 'img';  # we only look closer at <img ...>\npush(@imgs, values %attr);\n}\n\n# Make the parser.  Unfortunately, we don't know the base yet\n# (it might be different from $url)\n$p = HTML::LinkExtor->new(\\&callback);\n\n# Request document and parse it as it arrives\n$res = $ua->request(HTTP::Request->new(GET => $url),\nsub {$p->parse($[0])});\n\n# Expand all image URLs to absolute ones\nmy $base = $res->base;\n@imgs = map { $ = url($, $base)->abs; } @imgs;\n\n# Print them out\nprint join(\"\\n\", @imgs), \"\\n\";\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "HTML::Parser, HTML::Tagset, LWP, URI::URL\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright 1996-2001 Gisle Aas.\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "HTML::LinkExtor - Extract links from an HTML document",
    "flags": [],
    "examples": [
        "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->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 <img ...>",
        "push(@imgs, values %attr);",
        "# Make the parser.  Unfortunately, we don't know the base yet",
        "# (it might be different from $url)",
        "$p = HTML::LinkExtor->new(\\&callback);",
        "# Request document and parse it as it arrives",
        "$res = $ua->request(HTTP::Request->new(GET => $url),",
        "sub {$p->parse($[0])});",
        "# Expand all image URLs to absolute ones",
        "my $base = $res->base;",
        "@imgs = map { $ = url($, $base)->abs; } @imgs;",
        "# Print them out",
        "print join(\"\\n\", @imgs), \"\\n\";"
    ],
    "see_also": []
}