# Extracing the URL from hpricot element

**URL:** <https://www.ruby-forum.com/t/extracing-the-url-from-hpricot-element/155343>\
**Category:** Ruby\
**Created:** [December 10, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/extracing-the-url-from-hpricot-element/155343 "2008-12-10T19:26:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nratlos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@nratlos](https://www.ruby-forum.com/u/nratlos)\
**Post date:** [December 10, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/extracing-the-url-from-hpricot-element/155343/1 "2008-12-10T19:26:40Z")

</div>

I want to get a list of URLs from a webpage as follows:

First I create the Hpricot element as follows  
doc = Hpricot(open(searchurl))

links = doc/"//html//body//div[6]//div[2]//a[@id=‘p-1’]" +#

Next I want to append the URLs to an array as such:

results \<\< links.map.each{|link| puts link.attributes[‘href’] }

The line nicely prints out the URLs how I need them, but then  
puts the whole HTML link in the results array.

Any ideas how to get the URLs (without the HTML) into my results array ?

---

<div class="post-metadata">

**Author:** ![nratlos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ee59a6/32.png) [@nratlos](https://www.ruby-forum.com/u/nratlos)\
**Post date:** [December 10, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/extracing-the-url-from-hpricot-element/155343/2 "2008-12-10T19:56:09Z")

</div>

Use inject:

require ‘hpricot’  
require ‘open-uri’

doc = Hpricot(open(“[Ruby Programming Language](http://www.ruby-lang.org/en/)”))

(doc/“//a”).inject([]) do |links,anchor|  
Â links \<\< anchor.attributes[‘href’]  
end
