Howdy gang,

I'm working on a university website that has a list of links to scholarships for students. I've been handed the task of making sure all these links are "working," which as far as I'm concerned means they go somewhere.

I've been working on a script using WWW::Mechanize to:

  1. loop through a list of the pages that contain these links
  2. loop through the current page's links and use ->follow_link( text_regex => q/$_/i to find and follow the current link
  3. check the status of the last request using ->success
  4. pushing unsuccessful (where ->success returns a 0) requests for links into an array called @bad_links
  5. and finally, writing @bad_links to a text file.

When I run the script I've written, it seems to get stuck in an infinite loop with the first link.

No errors, and I don't really know what could be holding it up.

Here is the source listing, which isn't pretty but will hopefully eventually do the job:

#!/usr/bin/perl -w use strict; ## initialize the objects that we need use WWW::Mechanize; ## used to fetch the page we want my $mech = WWW::Mechanize->new(); ## our ::Mechanize obje +ct ## initialize an array of "bad" links ## we'll write this to a file when we're done my @bad_links; ## site root my $site_root = "http://www.mscd.edu/~women/scholarships/"; ## array of URLs to check ## probably wanna stick these in a file in the future my @urls_to_check = ('schola-f.shtml', 'scholg-l.shtml', 'scholm-r.sht +ml', 'schols-z.shtml'); my $bad_links_file = "badlinks.txt"; ## Start! ## loop through our urls we need to check for ( @urls_to_check ) { print "Getting $site_root$_...\n"; $mech->get( $site_root . $_ ); if ( $mech->success ) { print "Successfully retrieved $site_root$_\n"; } else { print "Couldn't retrieve $site_root$_!\n"; } ## loop through our list of links while ( $mech->links ) { print "Following $_\n"; $mech->follow_link( text_regex => qr/$_/i ); ## we need to either move on to the next link if this one is ## successful or push it into the @bad_links array if it isn't if ( $mech->success ) { print "Successfully followed $_\n"; } else { push @bad_links, $_; print "Unsuccessful in retrieving $_, moving on\n"; } } } print "Finished checking links. Writing results.\n"; open (BADLINKS, '>>', $bad_links_file); for ( @bad_links ) { print BADLINKS $_ . "\n"; } close (BADLINKS); ## Finished!

Thanks in advance!

meh.

In reply to Logging URLs that don't return 1 with $mech->success by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.