# New to ruby.. need a code

**URL:** <https://rubytalk.org/t/new-to-ruby-need-a-code/26324>\
**Category:** ruby-talk\
**Created:** [1 April 2006 11:03 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324 "2006-04-01T11:03:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rati\_lion](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@rati\_lion](https://rubytalk.org/u/rati_lion)\
**Post date:** [1 April 2006 11:03 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/1 "2006-04-01T11:03:49Z")

</div>

Hi all

I am new to Ruby. Found it as an intersting language. Can anyone help  
me with a simple code in Ruby to check for all the dead and live links  
in a website ?

Thanks  
Rati

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [1 April 2006 14:39 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/2 "2006-04-01T14:39:41Z")

</div>

A wonderful language, a sort of rough request here is some (very)  
simple code to get you started, using some great ruby libraries,  
including rubyful soup ([http://www.crummy.com/software/RubyfulSoup/\](http://www.crummy.com/software/RubyfulSoup/%5C)),  
which is available as a gem. As written it only checks one page, you  
would need to make it "walk" the links to recursively check a whole  
site.

Hope it helps  
pth

require 'open-uri'  
require 'uri'  
require 'rubyful\_soup'

url = '[http://www.yahoo.com/&#39](http://www.yahoo.com/&#39);  
uri = URI.parse(url)  
html = open(uri).read  
soup = BeautifulSoup.new(html)

#Search the soup  
links = soup.find\_all('a').map { |a| a['href'] }

# Remove javascript  
links.delete\_if { |href| href =~ /javascript/ }

links.each do |l|  
&nbsp;&nbsp;# resolve relative paths (there is probably a better way)  
&nbsp;&nbsp;link = URI.parse(l)  
&nbsp;&nbsp;link.scheme = 'http' unless link.scheme  
&nbsp;&nbsp;link.host = uri.host unless link.host  
&nbsp;&nbsp;link.path = uri.path + link.path unless link.path[0] == ?/  
&nbsp;&nbsp;link = URI.parse(link.to\_s)

&nbsp;&nbsp;# check the link  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;open(link).read  
&nbsp;&nbsp;&nbsp;&nbsp;# if we made it here the link is probably good  
&nbsp;&nbsp;rescue Exception =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;puts "#{link.to\_s}: #{e.to\_s}"  
&nbsp;&nbsp;end  
end

> **···**
>
> On 4/1/06, rati\_lion@yahoo.com \<rati\_lion@yahoo.com\> wrote:
> 
> > Hi all
> > 
> > I am new to Ruby. Found it as an intersting language. Can anyone help  
> > me with a simple code in Ruby to check for all the dead and live links  
> > in a website ?
> > 
> > Thanks  
> > Rati

---

<div class="post-metadata">

**Author:** ![rati\_lion](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@rati\_lion](https://rubytalk.org/u/rati_lion)\
**Post date:** [1 April 2006 16:18 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/3 "2006-04-01T16:18:50Z")

</div>

Thank you. Will take care in future to not post a rough request 🙂

Thanks again for this extended help.

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [1 April 2006 20:06 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/4 "2006-04-01T20:06:36Z")

</div>

Dňa Sobota 01 Apríl 2006 16:39 Patrick Hurley napísal:

> **···**
>
> > On 4/1/06, rati\_lion@yahoo.com \<rati\_lion@yahoo.com\> wrote:  
> > \> Hi all  
> > \>  
> > \> I am new to Ruby. Found it as an intersting language. Can anyone help  
> > \> me with a simple code in Ruby to check for all the dead and live links  
> > \> in a website ?  
> > \>  
> > \> Thanks  
> > \> Rati
> > 
> > A wonderful language, a sort of rough request here is some (very)  
> > simple code to get you started, using some great ruby libraries,  
> > including rubyful soup ([http://www.crummy.com/software/RubyfulSoup/\](http://www.crummy.com/software/RubyfulSoup/%5C)),  
> > which is available as a gem. As written it only checks one page, you  
> > would need to make it "walk" the links to recursively check a whole  
> > site.
> > 
> > Hope it helps  
> > pth
> > 
> > require 'open-uri'  
> > require 'uri'  
> > require 'rubyful\_soup'
> > 
> > url = '[http://www.yahoo.com/&#39](http://www.yahoo.com/&#39);  
> > uri = URI.parse(url)  
> > html = open(uri).read  
> > soup = BeautifulSoup.new(html)
> > 
> > #Search the soup  
> > links = soup.find\_all('a').map { |a| a['href'] }
> > 
> > # Remove javascript  
> > links.delete\_if { |href| href =~ /javascript/ }
> > 
> > links.each do |l|  
> > &nbsp;&nbsp;# resolve relative paths (there is probably a better way)  
> > &nbsp;&nbsp;link = URI.parse(l)  
> > &nbsp;&nbsp;link.scheme = 'http' unless link.scheme  
> > &nbsp;&nbsp;link.host = uri.host unless link.host  
> > &nbsp;&nbsp;link.path = uri.path + link.path unless link.path[0] == ?/  
> > &nbsp;&nbsp;link = URI.parse(link.to\_s)
> > 
> > &nbsp;&nbsp;# check the link  
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;open(link).read  
> > &nbsp;&nbsp;&nbsp;&nbsp;# if we made it here the link is probably good  
> > &nbsp;&nbsp;rescue Exception =\> e  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "#{link.to\_s}: #{e.to\_s}"  
> > &nbsp;&nbsp;end  
> > end
> 
> Yay for intuitive and descriptive class and library names, eh?
> 
> David Vallner

---

<div class="post-metadata">

**Author:** ![Robin\_Stocker1](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@Robin\_Stocker1](https://rubytalk.org/u/Robin_Stocker1)\
**Post date:** [2 April 2006 01:55 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/5 "2006-04-02T01:55:03Z")

</div>

Hi,

> links.each do |l|  
> &nbsp;&nbsp;# resolve relative paths (there is probably a better way)

Yes there is, see below 🙂

> &nbsp;&nbsp;link = URI.parse(l)  
> &nbsp;&nbsp;link.scheme = 'http' unless link.scheme  
> &nbsp;&nbsp;link.host = uri.host unless link.host  
> &nbsp;&nbsp;link.path = uri.path + link.path unless link.path[0] == ?/  
> &nbsp;&nbsp;link = URI.parse(link.to\_s)

link = uri.merge(l)

&nbsp;&nbsp;&nbsp;Robin

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [2 April 2006 00:41 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/6 "2006-04-02T00:41:01Z")

</div>

You are very welcome. And you are always welcome to request help. So I  
don't come off sounding too harsh, let me explain. This is a friendly  
community, generally populated by programmers and people striving to  
become programmers, so when asking for assistance in this manner, you  
will often receive more assistance when you show that you have spent a  
little time attempting to do this yourself, or asking for guidance on  
how to get started.

As for the name rubyful\_soup there are other better named libraries,  
but this is based upon a Python library (same author) called Beautiful  
Soup -- its claim to fame is that it will handle invalid markup, that  
will give most other parsers fits (so it is useful in the real world  
when dealing with other peoples "html"). I knew the library because I  
had read about it here.

Good luck again  
pth

> **···**
>
> On 4/1/06, rati\_lion@yahoo.com \<rati\_lion@yahoo.com\> wrote:
> 
> > Thank you. Will take care in future to not post a rough request 🙂
> > 
> > Thanks again for this extended help.

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [2 April 2006 02:59 UTC](https://rubytalk.org/t/new-to-ruby-need-a-code/26324/7 "2006-04-02T02:59:45Z")

</div>

Thanks I knew there had to be :-), I don't use URI much.

pth

> **···**
>
> On 4/1/06, Robin Stocker \<robin@nibor.org\> wrote:
> 
> > Hi,
> > 
> > \> links.each do |l|  
> > \> # resolve relative paths (there is probably a better way)  
> > Yes there is, see below 🙂
> > 
> > \> link = URI.parse(l)  
> > \> link.scheme = 'http' unless link.scheme  
> > \> link.host = uri.host unless link.host  
> > \> link.path = uri.path + link.path unless link.path[0] == ?/  
> > \> link = URI.parse(link.to\_s)  
> > link = uri.merge(l)
> > 
> > &nbsp;&nbsp;&nbsp;Robin
