是否可以使用Ruby从Filetype字段下载文件?

时间:2013-01-09 19:20:59

标签: ruby selenium ruby-1.9.3

我正在寻找一种从不同页面下载文件并将其存储在本地计算机中的特定文件夹下的方法。我使用的是Ruby 1.9.3

请参阅以下字段:

Filetypefield

File prompt to download

修改

这里是html内容:

<input type="hidden" name="supplier.orgProfiles(1152444).location.locationPurposes().extendedAttributes(Upload_RFI_Form).value.filename" value="Screenshot.docx">

<a style="display:inline; position:relative;" href="

                                      /aems/file/filegetrevision.do?fileEntityId=8120070&cs=LU31NT9us5P9Pvkb1BrtdwaCrEraskiCJcY6E2ucP5s.xyz">
                                Screenshot.docx
                             </a>

我刚试过的一个Possiblity: ,如果添加说https://xyz.test.com并构建如下所示的网址,则使用html内容

https://xyz.test.com/aems/file/filegetrevision.do?fileEntityId=8120070&cs=LU31NT9us5P9Pvkb1BrtdwaCrEraskiCJcY6E2ucP5s.xyz

并将该网址放到浏览器上并点击Enter,让我有机会按照提到的屏幕截图下载该文件。

现在可以通过脚本使用Ruby来完成吗?

1 个答案:

答案 0 :(得分:1)

由于您没有提供有效的网址,因此很难为您测试解决方案。

通常,检索URL的内容是相同的,无论是页面还是文件。 Ruby的内置OpenURI是快速路径:

require 'open-uri'
file = open('http://example.com').read

保存该文件很简单:

IO.binwrite('/path/to/file_to_save', file)

使用binwrite可以避免在保存二进制数据时发生的任何行结束转换。对于文本数据使用:

IO.write('/path/to/file_to_save', file)

IO.binwriteIO.write都记录在IO module