Extracing the URL from hpricot element


#1

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 ?


#2

Use inject:

require ‘hpricot’
require ‘open-uri’

doc = Hpricot(open(“http://www.ruby-lang.org/en/”))

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