1

I am trying to extract all the URLs from the raw output of some Ruby code:

require 'open-uri'

reqt = open("http://www.google.com").read
reqt.each_line { |line|
 if line =~/http/ then
 puts URI.extract(line)
 end }  

What am I doing wrong? I am getting extra lines along with URLs.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0xr3d0c
  • 35
  • 3
  • See [this comment](http://stackoverflow.com/questions/3665072/extract-url-from-text#comment29789408_9716632) on the duplicated question's [most popular answer](http://stackoverflow.com/a/9716632/182590). – Mark Thomas Aug 02 '14 at 13:55

2 Answers2

1

You can do this instead:

require 'open-uri'
reqt = open("http://www.google.com").read
urls = reqt.scan(/[[:lower:]]+:\/\/[^\s"]+/)
konsolebox
  • 72,135
  • 12
  • 99
  • 105
1

Remember the URL doesn't have to start with "http" - it could be a relative URL, the path to the current page. IMO it is the best to use Nokogiri to parse the HTML:

require 'open-uri'
require 'nokogiri'
reqt = open("http://www.google.com")
doc = Nokogiri::HTML(reqt)
doc.xpath('//a[@href]').each do |a|
  puts a.attr('href')
end

But if you really want to find only the absolute URLs, add a simple condition:

 puts a.attr('href') if a.attr('href') =~ /^http/i
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Grych
  • 2,861
  • 13
  • 22