- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了能够从 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)
但这仍然不太正确,我不知道网站所有者可以使用什么规则......
最佳答案
URLencode
跟在 RFC1738 specification 后面(参见第 2.2 节,第 3 页),其中指出:
only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a 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
。
关于r - R中的URLencode问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21977480/
我正在编写一个 Web 应用程序并学习如何对 html 链接进行 urlencode... 这里的所有 urlencode 问题(参见下面的标签)都是“如何...?”问题。 我的问题不是“如何?”但“
Dart 中有进行 urlencoding 的函数吗?我正在使用 XMLHttpRequest 对象进行 AJAX 调用,并且需要对 url 进行 url 编码。 我在 dartlang.org 上进
我们有一些非常基本的 mod_rewrite 规则: RewriteRule ^something.swf$ http://www.newdomain.com/something.swf [R=302
Server.UrlEncode 和 HttpUtility.UrlEncode 之间有区别吗? 最佳答案 我之前对这些方法感到非常头疼,我建议您避免 UrlEncode 的任何变体,而是使用 Uri
在PHP中: php -r "echo urlencode('["IM"]'); " 结果是%5BIM%5D 但是在java中 String text = URLEncoder.encode('["
From Python 的 urllib.urlencode 的 java 等价物是什么? 喜欢 >>> urllib.urlencode({'abc':'d f', 'def': '-!2'}) '
我发现了一个奇怪的问题,即 urlencoding 行为不一致。 更新 Spring MVC 4.3 和 5.1 版本之间存在差异: // FAIL in MVC 4.x @Test public v
express 框架带有 express 函数 express.json() 和 express.urlencoded()。 我们有 body-parser 库,它似乎具有相同的功能。 我的问题是,b
所以我正在处理的站点有一个过滤系统,它通过一个查询字符串传递一个键和值系统来运行。 整个站点很快就会进行重构,我正在维护现有站点,因此在我们讨论实现这一点的正确方法之前,我只需要更改分隔符的想法。 现
Dart中有进行urlencoding的功能吗?我正在使用XMLHttpRequest对象进行AJAX调用,我需要对该URL进行URL编码。 我在dartlang.org上进行了搜索,但没有得到任何结
我正在创建一个涉及将中文字符作为 url 参数发送的 php 应用程序。 我必须发送如下查询: http://xyz.com/?q=New 但是 xyz.com 的脚本不会自动对中文字符进行编码。因此
全部, 有个文字区说 如果用户给出输入, here is my name and my mail id is "a@x.com" 当数据在服务器端发布时,数据被接收为 这是我的名字,我的邮件
我想使用java的URLEncoder class编码我的 url 字符串。但是,默认情况下它将空格转换为“+”(加号)。我希望它改用“-”(类似于本网站对其网址进行编码的方式)。 我想知道最好的方法
我想对以下内部模板进行 url 编码 {{address.street}} {{address.city}} {address.state}} 无论如何在模板端执行此操作并将其放入href(我不希望将
我想在控制台应用程序VB.NET,VS 2010 Beta 2中使用HttpUtility.UrlEncode。 System.Web.HttpUtility.UrlEncode(item) 错误消息
我有一个服务堆栈应用程序,一个测试服务收到一个简单的请求,但是我发现收到的请求的值与原始请求不匹配。 我发:http://localhost/testapp/test?value=%22test%20
有没有办法对路径中的目录分隔符/进行 urlencode ? 喜欢 urlencode('/this/is/my/file right here.jpg'); 最佳答案 你可以使用 explode将您
是否可以在不转换 # 或 % 的情况下运行 urlencode 函数? 最佳答案 你能不能只做: $str = urlencode($str); $str = str_replace("%23", "
我正在尝试使用可选参数 shown in the docs 对 Django 1.3 中包含斜杠的字符串进行 URL 编码: {{ someString|urlencode:"" }} 但是,斜杠不会
我无法快速找到谷歌答案,而且我面前没有帮助(长话短说),所以,我的问题是:是否有一种简单的方法可以在 MS Visual 中对字符串进行 URL 编码FoxPro 8 最佳答案 这是一个可以满足您需要
我是一名优秀的程序员,十分优秀!