문제

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?
도움이 되었습니까?

해결책

  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.

다른 팁

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}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top