ironsand
ironsand

Reputation: 15171

How to use wget with `&` from ruby

I'm trying to get a web page data like http://example.com/foo.php?arg1=1&arg2=2.

I can get the page by using wget without problem, but when I call wget from ruby script like:

`wget http://example.com/foo.php?arg1=1&arg2=2`

then wget only connect to http://example.com/foo.php?arg1=1. In short wget ignores second argument.

I also tried with system method, but it ends up same error. How can I use & with wget from ruby?

Upvotes: 2

Views: 1181

Answers (1)

falsetru
falsetru

Reputation: 369334

Surround the url with quote to prevent shell interpreter &:

`wget 'http://example.com/foo.php?arg1=1&arg2=2'`
#     ^                                        ^

or escape the &:

`wget http://example.com/foo.php?arg1=1\\&arg2=2`
#                                      ^^^

UPDATE or you can use Shellwords::shellescape as Зелёный suggested:

"foo&bar".shellescape
# => "foo\\&bar"

require 'shellwords'
`wget #{Shellwords.shellescape('http://example.com/foo.php?arg1=1&arg2=2')}`

Or, use IO::popen (or Open3::*) which does not require escape:

IO.popen(['wget', 'http://example.com/foo.php?arg1=1&arg2=2']).read

Upvotes: 3

Related Questions