开发者

How to open URLs in rails?

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!


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

See the docs for more info.


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/">Some Link</a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜