views:

45

answers:

2

I'm trying to read in the html of a certain website.

Trying @something = open("http://www.google.com/") fails with the following error:

Errno::ENOENT in testController#show

No such file or directory - http://www.google.com/

Going to http://www.google.com/, I obviously see the site. What am I doing wrong?

Thanks!

+2  A: 

You need to require 'open-uri' first to be able to open() remote paths.

See the docs for more info.

rspeicher
Thank you. Where would I require it? Directly in the controller?
yuval
Yeah. You can put it at the top of the file.
rspeicher
+1  A: 

You should use a utility like Nokogiri to parse the returned content like so:

(From the Nokogiri site front page @ http://nokogiri.org/)

require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML:Document for the page we’re interested in...

doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))

# Do funky things with it using Nokogiri::XML::Node methods...

# Search for nodes by css
doc.css('h3.r a.l').each do |link|
  puts link.content
end

will print to the screen:

<a href="http://some.link/"&gt;Some Link</a>
Patrick Klingemann
Thanks. I actually am using Nokogiri, but wanted to provide a simplified example since the problem was with `open()`, not Nokogiri itself.
yuval