Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

scot (6695)

  (email not shown publicly)

Perl hacker
"apprentice" sysadmin for ASP running Sparc-Solaris-Java-Oracle stack

Journal of scot (6695)

Tuesday January 02, 2007
04:35 PM

Simple extraction of links from web page

[ #32052 ]

It took me far longer than I thought it would to come up with this code that grabs a web page and stuffs all the page's hyperlinks into a text file.

use strict;
use WWW::Mechanize;
#usage perl > output.txt
        my $url = shift;
                my $mech = WWW::Mechanize->new();
        my $status=$mech->status();
        print $status." OK-URL request succeeded."."\n";
        my @links = $mech->links;
        print STDOUT ($_->url, $/) foreach $mech->links;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I've now made this much easier in HTML::SimpleLinkExtor 1.14:

    linktractor -f= > output.txt
    No need to work too hard, after all. :)
    • Thank you. Can you see any obvious snafu's in the following code?:

      use strict;
      use warnings;
      use HTML::SimpleLinkExtor;
      use WWW::Mechanize qw( );

      #usage linkextractor -f [] > output.txt

      my ($url) = @ARGV;

      my $mech = WWW::Mechanize->new();
      my $response = $mech->get($url);
            or die($response->status_line() . "\n");

      my $extor = HTML::SimpleLinkExtor->new();
      my @all_links = $extor->links;
      foreach my $elem (@all