2

I browsed the documentation Mechanize. The below is for .pdf only.

require 'mechanize'

agent = Mechanize.new
agent.pluggable_parser.pdf = Mechanize::FileSaver
agent.get 'http://example.com/foo.pdf'
  1. But can I also download .docx,.xlsx,.txt file also?
  2. when the file download will be done,what would be it's default directory? Can we change the save file directory too?
  3. which browser would it select during downloading? Can we also change the browser control?
DoLoveSky
  • 777
  • 1
  • 6
  • 17

2 Answers2

2
  1. The type of file doesn't matter; any file accessible over the net can be obtained via mechanize, which is a tool for automating interaction with Mechanize.

  2. The file will be stored in the directory where the program was run. Use Mechanize::Download instead of Mechanize::FileSaver to specify where the file should be downloaded to. Example code here: https://stackoverflow.com/a/9105153/429758 (Specify the full path in the filename)

  3. Mechanize doesn't use a browser while downloading. For all intents and purposes, Mechanize acts like a web browser with no user interface via http://ruby.about.com/od/tasks/a/The-Mechanize-2-0-Handbook.htm

Do checkout the EXAMPLES page on mechanize documentation for further examples about how to use mechanize.

Community
  • 1
  • 1
Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • Any Good suggestion would you like to me give regarding - How to use the docs? I am talking about this as, I am first time seeing such kind of docs. Basically I am from Oracle domain. The basic trouble I am suffering with the doc is that,not all the procedures has a tiny code to explain the syntax. So how to do such homework, to understand what proceudre can be used how inside a code. The doc has such tiny codes with small amount of procedures, not for all. Any suggetions on that. I am really eager to know this platform,but the documentation sometime helps,sometime not. – DoLoveSky Jan 21 '13 at 10:14
  • 1
    For any subject in Ruby/Rails, the first thing I do is to check if there is a `RailsCasts` episode on that. Here is the one for mechanize: http://railscasts.com/episodes/191-mechanize That gives a good intro; after that it will be easier to understand the official documentation. – Prakash Murthy Jan 21 '13 at 10:17
  • Okay! this question is not subjective still would like to ask you that - Are you from `India`? :) I am from Kolkata,India. – DoLoveSky Jan 21 '13 at 10:19
  • I am done Sir. Really the nice flavour i am not getting due to that the immaturity fact of mine, with the documentation. With each of the procedure they have provided the source code,but not a tiny syntactical example. That's the pain for the new learner- basically who are doing self study! – DoLoveSky Jan 21 '13 at 10:25
2

It might be more straightforward to just save your file like so:

File.open('myfname.pdf', 'wb'){|f| f << agent.get('http://example.com/foo.pdf').body}
pguardiario
  • 53,827
  • 19
  • 119
  • 159