Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save image with Mechanize and Nokogiri?

I'm using Mechanize and Nokogiri to gather some data. I need to save a picture that's randomly generated at each request.

In my attempt I'm forced to download all pictures, but the only one I really want is the image located within div#specific.

In addition, is it possible to generate Base64 data from it, without saving it, or reloading its source?

require 'rubygems'
require 'mechanize'
require 'nokogiri'

a = Mechanize.new { |agent|
    agent.keep_alive = true
    agent.max_history = 0
}

urls = Array.new()
urls.push('http://www.domain.com');

urls.each {|url|

    page = a.get(url)
    doc = Nokogiri::HTML(page.body)

    if doc.at_css('#specific')

        page.images.each do |img|
          img.fetch.save('picture.png')
        end

    end
}
like image 542
charliexx Avatar asked Feb 13 '13 19:02

charliexx


1 Answers

To fetch the images from the specific location:

agent = Mechanize.new
page = agent.get('http://www.domain.com')
images = page.search("#specific img")

To save the image:

agent.get(images.first.attributes["src"]).save "path/to/folder/image_name.jpg"

To get image encoded without saving:

encoded_image = Base64.encode64 agent.get(images.first.attributes["src"]).body_io.string

I ran this just to make sure that the image that was encoded can be decoded back:

File.open("images/image_name.jpg", "wb") {|f| f.write(Base64.decode64(encoded_image))}
like image 189
victmask Avatar answered Sep 20 '22 09:09

victmask