【问题标题】:How to URL Encode a Backslash with R/RCurl如何使用 R/RCurl 对反斜杠进行 URL 编码
【发布时间】:2014-12-05 21:41:25
【问题描述】:

我目前正在尝试对字符串进行编码以插入 URL。我的问题是,当我的字符串包含反斜杠时,这似乎失败了。到目前为止,我已经使用 URLencode、curlEscape(来自 RCurl)和 curlPercentEncode(来自 RCurl)函数尝试了 4 种方法,但都没有成功。

> URLencode("hello\hello")
Error: '\h' is an unrecognized escape in character string starting ""hello\h"
> curlEscape("hello\hello")
Error: '\h' is an unrecognized escape in character string starting ""hello\h"
> curlPercentEncode("hello\hello")
Error: '\h' is an unrecognized escape in character string starting ""hello\h"
> curlPercentEncode("hello\hello", amp=TRUE)
Error: '\h' is an unrecognized escape in character string starting ""hello\h"

【问题讨论】:

  • 你试过URLencode("hello\\hello")吗?

标签: r urlencode rcurl


【解决方案1】:

你正在寻找

> URLencode("hello\\hello")
[1] "hello%5chello"

你得到的错误不是你试图调用的任何函数;它并没有真正调用它们中的任何一个。错误来自 R 的解析器。反斜杠是 字符串文字本身的特殊字符, 所以你需要在你的字符串文字中写一个 double 反斜杠以产生一个包含反斜杠的字符串值。 (您可以在反斜杠之后有用地放置其他几项内容;例如,"\"" 是您编写由一个双引号字符组成的字符串值的方式。阅读?Quotes 了解更多信息。)

由于这是字符串文字的语法问题,如果您从数据源读取“用于插入 URL 的字符串”,则不应出现此问题;如果您需要直接在代码中编写此字符串,这应该只是一个问题。

【讨论】:

  • 在我这边,我在某个特定点上被阻止了。我正在尝试将 http 基本凭据放入链接中:login:password@host">。在我的情况下,密码包含 \ 字符并且您的提示不起作用。有什么想法吗?
  • @DamienC 请提出一个新问题。
猜你喜欢
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多