【问题标题】:Problems with URLencode in RR中的URLencode问题
【发布时间】:2014-02-24 01:48:12
【问题描述】:

为了能够从 R 访问 NIST Chemistry Webbook 数据库,我需要能够将一些查询传递给 URL 编码的网址。大多数情况下,这种转换与 URLencode() 一起工作得很好,但在某些情况下不能。一种失败的情况,例如是为了

query="Poligodial + 3-methoxy-4,5-methylenedioxyamphetamine (R,S) adduct, # 1"

我尝试使用

library(XML)
library(RCurl)
url=URLencode(paste0('http://webbook.nist.gov/cgi/cbook.cgi?Name=',query,'&Units=SI'))
doc=htmlParse(getURL(url),encoding="UTF-8")

但是,如果您在网络浏览器中尝试此网址 http://webbook.nist.gov/cgi/cbook.cgi?Name=Poligodial%20+%203-methoxy-4,5-methylenedioxyamphetamine%20(R,S)%20adduct,%20%23%201&Units=SI 它给出了未找到的名称。 显然,如果您尝试从 http://webbook.nist.gov/chemistry/name-ser.html 它需要 URL 编码的字符串

"http://webbook.nist.gov/cgi/cbook.cgi?Name=Poligodial+%2B+3-methoxy-4%2C5-methylenedioxyamphetamine+%28R%2CS%29+adduct%2C+%23+1&Units=SI"

有没有人知道在这种情况下我应该使用什么样的gsub 规则来获得相同类型的 URL 编码?还是有其他简单的解决方法?

我试过了

url=gsub(" ","+",gsub(",","%2C",gsub("+","%2B",URLencode(paste('http://webbook.nist.gov/cgi/cbook.cgi?Name=',query,'&Units=SI', sep="")),fixed=T),fixed=T),fixed=T)

但这仍然不太正确,我不知道网站所有者可以使用什么规则......

【问题讨论】:

    标签: r urlencode gsub


    【解决方案1】:

    URLencode 跟在 RFC1738 specification 之后(参见第 2.2 节,第 3 页),其中指出:

    只有字母数字、特殊字符“$-_.+!*'()”和 可以使用用于保留目的的保留字符 在 URL 中未编码。

    也就是说,它不编码加号、逗号或括号。所以它生成的 URL 理论上是正确的,但实际上并不正确。

    Scott 提到的httr 包中的GET 函数从RCurl 调用curlEscape,它对这些标点字符进行编码。

    GET 调用handle_url 调用modify_url 调用build_url 调用curlEscape。)

    它生成的网址是

    paste0('http://webbook.nist.gov/cgi/cbook.cgi?Name=', curlEscape(query), '&Units=SI')
    ## [1] "http://webbook.nist.gov/cgi/cbook.cgi?Name=Poligodial%20%2B%203%2Dmethoxy%2D4%2C5%2Dmethylenedioxyamphetamine%20%28R%2CS%29%20adduct%2C%20%23%201&Units=SI"
    

    这个seems to work OK

    httr 有很好的功能,您可能想开始使用它。只需将URLencode 换成curlEscape 即可。

    【讨论】:

    • 非常感谢 - 对我来说,这是一个更简单的解决方案,特别是因为我更喜欢 getURL() 而不是 GET() (对我来说,它在一些不稳定的互联网连接上似乎更强大)!感谢您的精彩解释!
    • 一个纯粹出于兴趣/好奇的问题,但确实让curlEscape()URLencode() 更可取吗?就像,在我看来,URLencode 有时会起作用; curlEscape 一直有效..
    【解决方案2】:

    这是你想要的吗?

    library(httr)
    url <- 'http://webbook.nist.gov/cgi/cbook.cgi'
    args <- list(Name = "Poligodial + 3-methoxy-4,5-methylenedioxyamphetamine (R,S) adduct, # 1",
             Units = 'SI')
    res <- GET(url, query=args)
    content(res)$children$html
    

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <meta http-equiv="Window-target" content="_top"/>
    
    ...etc.
    

    【讨论】:

    • 哈,这好多了,是的 - 非常感谢 - 正是它应该做的! Thx 数百万!
    【解决方案3】:

    @Richie Cotton 的解决方案也解决了#,而URLencode() 没有。

    这是一个非常简单的例子

    # Useless...
    URLencode("hi$there")
    [1] "hi$there"
    
    # This is good, but only if special characters are escaped first
    URLencode("hi\\$there")
    [1] "hi%5C$there"
    
    # This works without escaping!
    library(httr)
    curlEscape("hi$there")
    [1] "hi%24there"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多