1

Possible Duplicate:
How do I download a binary file over HTTP using Ruby?

I have an array called pdf_links containing links to PDF files collected by the Mechanize gem.

http://site/file1.pdf
http://site/file2.pdf
http://site/file3.pdf
http://site/file4.pdf
http://site/file5.pdf
http://site/file6.pdf
http://site/file7.pdf

I need to save all the PDF files to my directory.

What is the best way to do it with Ruby?

I tried to do it using *nix string but I'm receiving an error:

pdf_links.each do |d|
    system %x{ wget #{d} }
end
Community
  • 1
  • 1
jagga99
  • 47
  • 7

2 Answers2

3

There is a save method in mechanize to store it

Save WWW::Mechanize::File to disk using FileUtils

http://mechanize.rubyforge.org/Mechanize/File.html#method-i-save

or you can use ruby lib 'open-uri'

 require 'open-uri'
 pdf_links.each do |link|
  File.open(file_path_to_store, 'wb') {|f| f.write(open(link).read)}
 end
Community
  • 1
  • 1
Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51
2

Mechanize can do it:

agent.get(d).save
pguardiario
  • 53,827
  • 19
  • 119
  • 159