# Extract relative links from html page

**URL:** <https://www.ruby-forum.com/t/extract-relative-links-from-html-page/71223>\
**Category:** Ruby\
**Created:** [October 6, 2006, 6:26am UTC](https://www.ruby-forum.com/t/extract-relative-links-from-html-page/71223 "2006-10-06T06:26:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 6, 2006, 6:26am UTC](https://www.ruby-forum.com/t/extract-relative-links-from-html-page/71223/1 "2006-10-06T06:26:35Z")

</div>

Does somebody have any suggestions on how to extract relative as well  
as absolute links from an html page. It seems like the URI.extract only  
matches on absolute urls.  
Any pointers or suggestions are appreciated.  
THanks-  
Christian

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 6, 2006, 7:46am UTC](https://www.ruby-forum.com/t/extract-relative-links-from-html-page/71223/2 "2006-10-06T07:46:40Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Does somebody have any suggestions on how to extract relative as well  
> as absolute links from an html page. It seems like the URI.extract only  
> matches on absolute urls.

Roll your own, a classic piece of programming advice:

#!/usr/bin/ruby -w

data = File.read("/path/page.html")

data.scan(/src\s\*=\s\*"(.\*?)"/im) { |item|  
puts “src = #{item}\n”  
}

data.scan(/href\s\*=\s\*"(.\*?)"/im) { |item|  
puts “href = #{item}\n”  
}
