Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve contents of URL as string

Tags:

ruby

open-uri

For tedious reasons to do with Hpricot, I need to write a function that is passed a URL, and returns the whole contents of the page as a single string.

I'm close. I know I need to use OpenURI, and it should look something like this:

require 'open-uri'
open(url) {
  # do something mysterious here to get page_string
}
puts page_string

Can anyone suggest what I need to add?

like image 756
AP257 Avatar asked Jul 07 '10 09:07

AP257


3 Answers

You can do the same without OpenURI:

require 'net/http'
require 'uri'

def open(url)
  Net::HTTP.get(URI.parse(url))
end

page_content = open('http://www.google.com')
puts page_content

Or, more succinctly:

Net::HTTP.get(URI.parse('http://www.google.com'))
like image 166
Carlo Pecchia Avatar answered Nov 07 '22 19:11

Carlo Pecchia


The open method passes an IO representation of the resource to your block when it yields. You can read from it using the IO#read method

open([mode [, perm]] [, options]) [{|io| ... }] 
open(path) { |io| data = io.read }
like image 38
Jeriko Avatar answered Nov 07 '22 18:11

Jeriko


require 'open-uri'
open(url) do |f|
  page_string = f.read
end

See also the documentation of IO class

like image 12
Teoulas Avatar answered Nov 07 '22 18:11

Teoulas