Waffles
Waffles

Reputation: 349

Downloading a file in Ruby, from a site that generates a download link?

I'm trying to write a Ruby script that would download an image file from a website, which does not have an image on it initially. The way this site works normally is that when you access the site, the site waits for about a second while it generates the link to download the image. After that's done, the browser presents the user with a download window to actually download the image. At no point is the image presented on the site itself, the site always says "Please wait", or "your image is ready to download".

How can I write a quick and dirty ruby script to download these image file links to the user's desktop?

Upvotes: 1

Views: 190

Answers (2)

Duck1337
Duck1337

Reputation: 524

Use Watir and nokogiri

require 'watir-webdriver'
require 'nokogiri'

$browser = Watir::Browser.new
$browser.goto "google.com"

$page_html = Nokogiri::HTML.parse($browser.html)

image = []
image = $page_html.css("img#hplogo").map{|link| link['src']}[0]

image_src = "https://www.google.com" + image

File.open("/home/user/Desktop/image.png", 'wb') do |f|
    f.write open(image_src).read
end

Upvotes: 1

sawa
sawa

Reputation: 168269

Using the gem mechanize is a popular way of automating interaction with websites.

Upvotes: 0

Related Questions