Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
"be consistent"

How do I parse links out of a web page

( #13054=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on May 18, 2000 at 20:28 UTC
Q&A  > HTTP and FTP clients


I'm trying to parse all the links in a web page into an array organized like this: ($link, $description) where:
<a href="">Come <b>visit</b> my <u>we +b page</u>!</a>
gets parsed into: $link = "" $description = "Come visit my web page!" thanks very much for the help!

Answer: How do I parse links out of a web page
contributed by tokpela

Or you can use WWW::Mechanize

use strict; use warnings; use WWW::Mechanize; my $url = "file:///D:/webpage.html"; #my $url = ""; my $mech = WWW::Mechanize->new(); $mech->get( $url ); my @links = $mech->links(); foreach my $link (@links) { print "LINK: " . $link->url() . "\n"; print "DESCRIPTION: " . $link->text() . "\n"; }
Answer: How do I parse links out of a web page
contributed by gregorovius

Unfortunately HTML::LinkExtor does not offer a way of extracting the link text from the 'A' tag. You can resort to the HTML::TokeParser instead.

The HTML::TokeParser perldoc contains a snippet that does exactly what you ask for, except that the link URLs it extracts can be relative so you need to concatenate a base to them.

Answer: How do I parse links out of a web page
contributed by Anonymous Monk

You could try this as well

Answer: How do I parse links out of a web page
contributed by merlyn

See HTML::LinkExtor in the LWP module in the CPAN.

Answer: How do I parse links out of a web page
contributed by agent00013

The Perl Cookbook has a good example:

#!/usr/local/bin/perl # xurl - extract unique, sorted lists of links from URL use HTML::LinkExtor; use LWP::Simple; $base_url = shift; $parser = HTML::LinkExtor->new(undef, $base_url); $parser->parse(get($base_url))->eof; @links = $parser->links; foreach $linkarray (@links) { local(@element) = @$linkarray; local($elt_type) = shift @element; while (@element) { local($attr_name, $attr_value) = splice (@element, 0, 2); $seen{$attr_value}++; } } for (sort keys %seen) { print $_, "\n"}
Hope this helps. /msg me if you need anything else.


  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (5)
    As of 2014-03-16 17:04 GMT
    Find Nodes?
      Voting Booth?

      Have you used a cryptocurrency?

      Results (330 votes), past polls