我使用-O
上的--output-document
或wget
选项存储从网站获取http。但是-O选项需要存储输出的文件,我想将它存储在程序中的变量中,以便我可以更容易地操作它。如果没有从文件中重新读取它,有没有办法做到这一点?实质上,我手动创建一个原始缓存。
示例代码
#!/usr/bin/ruby
url= "http://www.google.com"
whereIWantItStored = `wget #{url} --output-document=outsideFile`
参考:
我发现这篇文章有助于在我的计划中使用wget
:Using wget via Ruby on Rails
答案 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