# New to ruby.. need a code

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394>\
**Category:** Ruby\
**Created:** [April 1, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394 "2006-04-01T13:06:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/1 "2006-04-01T13:06:13Z")

</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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/2 "2006-04-01T16:40:17Z")

</div>

On 4/1/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

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 ([Rubyful Soup: "The brush has got entangled in it!"](http://www.crummy.com/software/RubyfulSoup/)),  
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/](http://www.yahoo.com/)’  
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|

# resolve relative paths (there is probably a better way)

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)

# check the link

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/3 "2006-04-01T18:21:55Z")

</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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/4 "2006-04-01T22:08:50Z")

</div>

DÅ?a Sobota 01 AprÃ­l 2006 16:39 Patrick H. napÃ­sal:

> A wonderful language, a sort of rough request here is some (very)  
> require ‘uri’
> 
> # Remove javascript
> 
> # check the link
> 
> begin  
> open(link).read  
> # if we made it here the link is probably good  
> rescue Exception =\> e  
> puts “#{link.to\_s}: #{e.to\_s}”  
> end  
> end

Yay for intuitive and descriptive class and library names, eh?

David V.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2006, 3:57am UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/5 "2006-04-02T03:57:37Z")

</div>

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)

Robin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2006, 2:41am UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/6 "2006-04-02T02:41:46Z")

</div>

On 4/1/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you. Will take care in future to not post a rough request 🙂
> 
> Thanks again for this extended help.

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2006, 5:02am UTC](https://www.ruby-forum.com/t/new-to-ruby-need-a-code/93394/7 "2006-04-02T05:02:21Z")

</div>

On 4/1/06, Robin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > link = URI.parse(link.to\_s)  
> > link = uri.merge(l)
> 
> Robin

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

pth
