Search code examples
ruby-on-railsrubyputs

using 'puts' to get information from external domain


ive just started with ruby on rails the other day and i was wandering is it possible to using the puts function to get the content of a div from a page on an external page.

something like puts "http://www.example.com #about"

would something like this work ? or would you have to get the entire page and then puts that section that you wanted ?

additionaly if the content on the "example.com" #about div is constantly changing would puts constantly update its output or would it only run the script each time the page is refreshed ?


Solution

  • The open-uri library (for fetching the page) and the Nokogiri gem (for parsing and retrieving specific content) can assist with this.

    require 'open-uri'
    require 'nokogiri'
    
    doc = Nokogiri::HTML(open('http://www.example.com/'))
    
    puts doc.at('#about').text