Search code examples
ruby-on-railsruby-on-rails-pluginsrubygems

Is there a Rails gem or plugin to get a list of image URLs on a page, given the page's URL?


Is there a Rails gem or plugin to get a list of image URLs on a page, given the page's URL?


Solution

  • Use the Nokogiri gem to parse the page and then get the src attribute of all img tags that appear in the document:

    $ irb
    irb(main):001:0> require 'rubygems'
    irb(main):002:0> require 'nokogiri'
    irb(main):003:0> require 'open-uri'
    irb(main):004:0> doc = Nokogiri::HTML(open("http://stackoverflow.com/questions/4741550"))
    irb(main):006:0> doc.css('img').collect {|elem| elem[:src] }
    => ["http://www.gravatar.com/avatar/0543907746be29497b873de97957d3ab?s=32&d=identicon&r=PG",
        "/posts/4741550/ivc/9291",
        "http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif"]