Ruby文件下载

时间:2013-01-31 09:32:48

标签: ruby curl cucumber nokogiri mechanize

enter image description here

有没有办法访问此对话框以获取文件名或将此文件保存在某处,以便我以后可以访问它。我使用Ruby机制来浏览网站以进入此屏幕。

2 个答案:

答案 0 :(得分:4)

没有机械化对话框。您提交表单,返回一个Mechanize :: File对象,然后您可以像这样保存:

file = form.submit
File.open('myfile','w'){|f| f << file.body}

答案 1 :(得分:1)

我会这样做。

使用nokogiri打开页面:

@doc = Nokogiri::HTML(open(url)) 

浏览文档页面,找到要下载的链接。 然后你可以使用这个链接:

require 'net/http'

Net::HTTP.start('theserver.com') { |http| 
   resp = http.get('/xx/the_file_to_downlaod.csv')
      open('the_downlaod.csv', 'wb') { |file|
         file.write(resp.body)
      }
} 
相关问题