Ruby在变量中存储wget输出

时间:2013-04-19 01:45:22

标签: ruby wget

我使用-O上的--output-documentwget选项存储从网站获取http。但是-O选项需要存储输出的文件,我想将它存储在程序中的变量中,以便我可以更容易地操作它。如果没有从文件中重新读取它,有没有办法做到这一点?实质上,我手动创建一个原始缓存。

示例代码

#!/usr/bin/ruby
url= "http://www.google.com"
whereIWantItStored = `wget #{url} --output-document=outsideFile`

参考: 我发现这篇文章有助于在我的计划中使用wgetUsing wget via Ruby on Rails

2 个答案:

答案 0 :(得分:3)

#!/usr/bin/ruby
url= "http://www.google.com"
whereIWantItStored = `wget #{url} -O -`

请务必清理您的网址以避免注入外壳。 -O-after表示标准输出,由ruby反引号捕获。

https://www.owasp.org/index.php/Command_Injection解释了shell注入。

http://apidock.com/ruby/Shellwords/shellescape对于Ruby> = 1.9或者Escape Gem对于ruby 1.8.x

答案 1 :(得分:0)

我不会使用wget。我会使用类似HTTParty的内容。

然后你可以这样做:

require 'httparty'
url = 'http://www.google.com'
response = HTTParty.get(url)

whereIWantItStored = response.code = 200 ? response.body : nil