Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download File with Ruby Mechanize

Tags:

ruby

mechanize

I am trying to save a file with mechanize. The script I've included enables me to download a file with the correct name, but there is no content. Any ideas on what I might be doing wrong? Here is the resource that I am using.

http://www.rubydoc.info/gems/mechanize/Mechanize/File

Any help would be great! Thank you!

require 'mechanize'

uri = URI 'http://website.com/page.html'
file = Mechanize::File.new uri, nil, ''
filename = file.save
puts filename
like image 994
Brandon Avatar asked Mar 18 '23 09:03

Brandon


2 Answers

mecha = Mechanize.new
file = mecha.get(uri)
filename = file.save

You shouldn't be constructing the File object, since it's empty. It is intended to be constructed by Mechanize when it fetches an object.

like image 151
Amadan Avatar answered Mar 24 '23 08:03

Amadan


I've not see it used that way. Normally you need to create an agent, then issue the get.

try this

require 'rubygems'
require 'mechanize'

uri = URI 'http://website.com/page.html'
agent = Mechanize.new
file = agent.get uri

filename = file.save  # saves to page.html
puts filename         # page.html
like image 25
Doon Avatar answered Mar 24 '23 10:03

Doon