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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With