473,769 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl for checking broken link on a website

5 New Member
Hi Guys!


I am new on this forum and I am hoping that I could acquire help to improve my PERL skill.

My goal is to be able to create a report of all broken links from a website. Based from my understanding, PERL can go through to server directory and search for htm/html file and put it on an array. From this array I believe PERL can search for a "http://" string and emulate to click the URL. If the URL contains "Page cannot be found" then create and append to a log file.

My question, what is the best method to do this and what is the required library and syntax?

Also for searching the "htm" file on a server, assuming that I don't have access to get in to server directory (which is unlikely) , how do I get this done? and if I have an access to the server how do I get PERL to login to the server first before PERL search for "htm" file on a specific directory? or do I have to put the PERL .pl file on the server root directory and run it there?

I really appreciate if someone could help. Thank you.
Jan 24 '08 #1
11 2906
eWish
971 Recognized Expert Contributor
Welcome to TSDN!

I would suggest that you use a module like WWW::Mechanize or LWP::UserAgent to achieve that you are wanting. Also, checkout WWW::Mechanize: :FAQ.

--Kevin
Jan 24 '08 #2
eWish
971 Recognized Expert Contributor
You might also check out W3C::LogValidat or::LinkChecker . If I read it correctly it will use your server log to locate broken links.

--Kevin
Jan 24 '08 #3
sweetbox
5 New Member
Thanks for your feedback Kevin! anyone else?

I will try to post the sample code later on for your feedback and also for someone else to learn. Cheers!
Jan 24 '08 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
Thanks for your feedback Kevin! anyone else?

I will try to post the sample code later on for your feedback and also for someone else to learn. Cheers!
Considering that Kevin pretty much covered it, I doubt anyone else will be throwing out the same links that he did.

Regards,

Jeff
Jan 24 '08 #5
sweetbox
5 New Member
Guys I'm a bit stuck here.

I would like to know how to capture a certain word (in this case "http://") in the file content however I don't know what the syntax is. Could anyone help? Thanks.
Expand|Select|Wrap|Line Numbers
  1.  #Capturing .htm or .html filename on root directory
  2. my @root_files = <*.htm>;
  3. #Searching through directory
  4. for ($count=0;$count<@root_files;$count++)
  5. {
  6.     #opening the file one by one
  7.     my $FILENAME = @root_files[$count];
  8.     open(FILEPOINTER, "< $FILENAME") or die "Can't open $filename : $!";
  9.  
  10.     #Searching file content line by line
  11.     do
  12.     {
  13.         my $LINECONTENT = <FILEPOINTER>;
  14.                 # I need to capture only the http link if there is any. the "print" below is just for testing to make sure file content is shown line by line on command prompt
  15.         print $LINECONTENT;
  16.     }
  17.     until eof;
  18.     close FILEOPEN;    
  19. }
  20.  
Jan 25 '08 #6
numberwhun
3,509 Recognized Expert Moderator Specialist
Guys I'm a bit stuck here.

I would like to know how to capture a certain word (in this case "http://") in the file content however I don't know what the syntax is. Could anyone help? Thanks.
Expand|Select|Wrap|Line Numbers
  1.  #Capturing .htm or .html filename on root directory
  2. my @root_files = <*.htm>;
  3. #Searching through directory
  4. for ($count=0;$count<@root_files;$count++)
  5. {
  6.     #opening the file one by one
  7.     my $FILENAME = @root_files[$count];
  8.     open(FILEPOINTER, "< $FILENAME") or die "Can't open $filename : $!";
  9.  
  10.     #Searching file content line by line
  11.     do
  12.     {
  13.         my $LINECONTENT = <FILEPOINTER>;
  14.                 # I need to capture only the http link if there is any. the "print" below is just for testing to make sure file content is shown line by line on command prompt
  15.         print $LINECONTENT;
  16.     }
  17.     until eof;
  18.     close FILEOPEN;    
  19. }
  20.  
Ok, I think that the best thing for you to do is grab a good Perl book and start reading.

I say this because this line:

Expand|Select|Wrap|Line Numbers
  1. my @root_files = <*.htm>;
  2.  
does not do what you are expecting. You cannot just specify "*.htm" and expect all files with .htm on the end to magically be in the array.

First, what OS are you working on? Windows or Unix/Linux? That will help tell us how you are going to get your directory listing.

Regards,

Jeff
Jan 25 '08 #7
sweetbox
5 New Member
Thanks for your reply Jeff.

I'm working on Linux and the "my @root_files = <*.htm>" worked as long as I have the .htm file at the same directory as the .pl file. I guess I will also need to improve the code to automatically dig deeper into the directory if it contains more folders (need more time, my perl book is confusing, if you can give me a hint that'll be great!).

Right now I am working on the Regular Expression to grab only the http link

Expand|Select|Wrap|Line Numbers
  1.  
  2. do
  3.     {
  4.         my $LINECONTENT = <FILEPOINTER>;
  5.         $_ = $LINECONTENT;
  6.        #this is where I'm stuck at this stage. The code below list the whole line instead of just the http://
  7.  
  8.         if (/a href="?"/) 
  9.         {
  10.             print $LINECONTENT;
  11.         }
  12.     }
  13.     until eof;
  14.  
Jan 25 '08 #8
KevinADC
4,059 Recognized Expert Specialist
Hi Guys!


I am new on this forum and I am hoping that I could acquire help to improve my PERL skill.

My goal is to be able to create a report of all broken links from a website. Based from my understanding, PERL can go through to server directory and search for htm/html file and put it on an array. From this array I believe PERL can search for a "http://" string and emulate to click the URL. If the URL contains "Page cannot be found" then create and append to a log file.

My question, what is the best method to do this and what is the required library and syntax?

Also for searching the "htm" file on a server, assuming that I don't have access to get in to server directory (which is unlikely) , how do I get this done? and if I have an access to the server how do I get PERL to login to the server first before PERL search for "htm" file on a specific directory? or do I have to put the PERL .pl file on the server root directory and run it there?

I really appreciate if someone could help. Thank you.
Can't you just use something like FrontPage to do that? I am sure there are many third party programs you can use to report broken site links.
Jan 26 '08 #9
sweetbox
5 New Member
Ummm yes I guess you can use 3rd party software to check broken link but you won't learn anything...

I have just finished a very basic script although need a lot of improvement.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3. use LWP::UserAgent;
  4.  
  5.  
  6. #Capturing .htm file
  7. #Improvement required. How if there are multiple folders
  8. my @root_files = <*.htm>;
  9. #Searching through folder
  10. for ($count=0;$count<@root_files;$count++)
  11. {
  12.     #opening the file one by one
  13.     my $FILENAME = (@root_files[$count]);
  14.     open(FILEPOINTER, "< $FILENAME") or die "Can't open $filename : $!";
  15.  
  16.     #Searching file content line by line
  17.     do
  18.     {
  19.         my $LINECONTENT = <FILEPOINTER>;
  20.         $_ = $LINECONTENT;
  21.         if (/href="?"/i)
  22.         {
  23.             #capturing quoted strings in href
  24.             #Improvement required. How if there are multiple href in each line?
  25.             my $beginstring = index($LINECONTENT,'"');
  26.             my $endstring = index($LINECONTENT, '"', $beginstring + 1);
  27.             my $finalstring = substr($LINECONTENT,($beginstring+1),($endstring-$beginstring-1));
  28.  
  29.             my $ua = LWP::UserAgent->new;
  30.             $ua->timeout(3);
  31.  
  32.             #For each link found check for 404 error and log to text file #Improvement required. How if there are a relative URI?
  33.             my $response = $ua->get($finalstring);
  34.             if ($response->is_error)
  35.             { 
  36.                 #output the link that has 404 error
  37.                 open(TEXT,">>broken_link_output.txt");
  38.                 printf TEXT $finalstring. "\n";                
  39.             }
  40.             close TEXT;
  41.         }
  42.     }
  43.     until eof;
  44.     close FILEPOINTER;    
  45. }
  46.  
  47.  
Jan 26 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2236
by: | last post by:
I am planning to develop a directory website (ASP.NET) which will contain links to hundreds of external web pages. In an effort to keep the directory up to date, I would like to trap (perhaps as an event) and then log when the user clicks on a broken link (page not found or server not available). Keep in mind that these are links to 3rd party pages not hosted on my website or server. I think I can do this using client side Java scripting,...
0
9747
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
28
3339
by: Craig Cockburn | last post by:
I have a tool which tells me the number of times that visitors attempt to access a link from my site to an external site and what the response code received was. In the event of the remote site returning an error code, they are not sent to the remote site - why bother, it wouldn't work! Since I have over 1000 external links, this allows me to locate the broken links that people see the most often and fix those first. Conventional link...
2
8784
by: barrybevel | last post by:
Hi, I have a very small simple program below which does the following: 1) post a username & password to a website - THIS WORKS 2) follow a link - THIS WORKS 3) update values of 2 fields and post the form - ERROR! This works fine using firefox even with javascript turned off. But when using Perl (v5.8.8 on FC5) I get a page back stating an error has occured: "We're sorry, an error has occurred. Please review the error below There has...
21
34437
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
7
55138
numberwhun
by: numberwhun | last post by:
**NOTE: This article is written using the 5.8.8 Alpha2 release of Strawberry Perl. I am writing this article with much joy and glee. This is due to the fact that Active State no longer has a monopoly on the issue of Perl on the Windows platform As anyone who code's Perl on the Windows platform knows, the choice that you have (had) for Perl on Windows was Active State. It worked great for your scripting but when it came to installing...
10
6976
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration of section c. Not sure where went wrong as the web page displayed internal server error. Also, what is the error 543? and error 2114. Where to find the list of errors in websites as it is not the standard apache error. I could not find...
1
47484
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.