gpt4 book ai didi

ruby - 如何将wget与ruby的 `&`一起使用

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:36 25 4
gpt4 key购买 nike

我正在尝试获取类似 http://example.com/foo.php?arg1=1&arg2=2 的网页数据。

我可以毫无问题地使用 wget 获取页面,但是当我从 ruby​​ 脚本调用 wget 时,例如:

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

然后 wget 只连接到 http://example.com/foo.php?arg1=1。简而言之,wget 会忽略第二个参数。

我也尝试过使用 system 方法,但它以同样的错误结束。我如何将 & 与 ruby​​ 的 wget 一起使用?

最佳答案

用引号将 url 括起来以防止 shell 解释器 &:

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

或者转义&:

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

更新 或者您可以使用 Shellwords::shellescape正如Зелёный建议的那样:

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

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

或者,使用 IO::popen (或 Open3::* )不需要转义:

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

关于ruby - 如何将wget与ruby的 `&`一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26323024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com