我如何使用R和RCurl包编写以下curl POST方法?
curl -k -u myusername:mypassword -d '{"text":"Hello World!","level":"Noob"}' -H "Content-Type: application/json" -H "Accept: application/json" "http://api.website/v1/access?"
Run Code Online (Sandbox Code Playgroud) 我有这个问题:
> install.packages("RCurl")
Installing package(s) into ‘/home/username/R/x86_64-redhat-linux-gnu-library/2.13’
(as ‘lib’ is unspecified)
trying URL 'http://lib.stat.cmu.edu/R/CRAN/src/contrib/RCurl_1.95-3.tar.gz'
Content type 'application/x-gzip' length 868491 bytes (848 Kb)
opened URL
==================================================
downloaded 848 Kb
* installing *source* package ‘RCurl’ ...
checking for curl-config... no
Cannot find curl-config
ERROR: configuration failed for package ‘RCurl’
* removing ‘/home/username/R/x86_64-redhat-linux-gnu-library/2.13/RCurl’
Warning in install.packages :
installation of package 'RCurl' had non-zero exit status
The downloaded packages are in
‘/tmp/RtmpKyAgF8/downloaded_packages’
Run Code Online (Sandbox Code Playgroud)
和:
> install.packages("XML")
Installing package(s) into ‘/home/username/R/x86_64-redhat-linux-gnu-library/2.13’
(as ‘lib’ is unspecified)
trying …Run Code Online (Sandbox Code Playgroud) 我无法devtools在redhat linux服务器上的R Studio中安装软件包.出现这些错误消息:
ERROR: configuration failed for package ‘RCurl’
* removing ‘/home/xx/R/x86_64-redhat-linux-gnu-library/3.0/RCurl’
Warning in install.packages :
installation of package ‘RCurl’ had non-zero exit status
ERROR: dependency ‘RCurl’ is not available for package ‘httr’
* removing ‘/home/xx/R/x86_64-redhat-linux-gnu-library/3.0/httr’
Warning in install.packages :
installation of package ‘httr’ had non-zero exit status
ERROR: dependencies ‘httr’, ‘RCurl’ are not available for package ‘devtools’
Run Code Online (Sandbox Code Playgroud)
我也无法安装RCurl包.我也尝试过安装这些libcurl库:
sudo yum install libcurl4-openssl-dev
sudo yum install libcurl4-gnutls-dev
Run Code Online (Sandbox Code Playgroud)
但该系统表示没有可用的此类软件包.
有没有其他方法来安装devtools包?或者我该如何解决Rcurl安装问题?
尝试从特定网页下载信息,虽然它在任何浏览器中都可以正常使用,但RCurl表示它不存在:
url.exists("http://www.transfermarkt.es/liga-mx-apertura/startseite/wettbewerb/MEXA")
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
使用".de"时的结果相同.
url.exists("http://www.transfermarkt.de/liga-mx-clausura/startseite/wettbewerb/MEX1")
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
使用RCurl的其他功能时,它也会返回错误
> htmlParse("http://www.transfermarkt.es/liga-mx-apertura/startseite/wettbewerb/MEXA")
Error: failed to load HTTP resource
> htmlTreeParse("http://www.transfermarkt.es/liga-mx-apertura/startseite/wettbewerb/MEXA")
Error: failed to load HTTP resource
> htmlParse(getURL("http://www.transfermarkt.es/liga-mx-apertura/startseite/wettbewerb/MEXA"))
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr>
<center>nginx</center>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么会这样?如何在这个网页上成功使用htmlParse?
编辑:
我熟悉httr包,这很好用:
content(GET("http://www.transfermarkt.es/liga-mx-apertura/startseite/wettbewerb/MEXA"))
Run Code Online (Sandbox Code Playgroud) 我一直在使用 httr 和 rcurl,但无法设法将以下curl GET 请求转换为 R:
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer 31232187asdsadh23187' 'https://this.url.api.com:334/api/endpoint'
Run Code Online (Sandbox Code Playgroud)
特别是,我在传递授权选项时遇到了一些麻烦,因为我无法在两个库中找到等效的参数。这可能是一个自定义标头?
我正试图从图表API获取歌词.我编写了一个可以工作但不在循环内的R函数.我的脚本是:
library(httr)
library(RCurl)
library(XML)
df <- data.frame(artist = c('Led Zeppellin', 'Adele'), song = c('Rock´n roll', 'Hello'), stringsAsFactors = F)
make.querye <- function(xx) {
names_ok <- gsub(" ", "&", xx)
names_ok2 <- paste("\'", names_ok, "\'", sep = '')
querye <- paste("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=", names_ok[1],"&song=", names_ok[2], sep='')
data <- GET(querye)
aa <- content(data, "text")
doc <- htmlParse(aa, asText=TRUE)
plain.text <- xpathSApply(doc, "//lyric//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue)
if (length(plain.text)==0) {
plain.text2 <- 'Lyrics not found'
} else {
plain.text2 <- iconv(plain.text, from = "UTF-8", to = …Run Code Online (Sandbox Code Playgroud) 我们使用Windows身份验证(Kerberos)在IIS中设置了数据服务器.有没有人知道如何从R身份验证Web身份服务(GET和POST)?
在RCurl中,CFILE定义了一个函数和一个类来处理C级文件句柄.从手册:
目的是能够将这些作为选项传递给libcurl,以便它可以从文件读取或写入文件.我们也可以使用R连接执行此操作并指定操作这些连接的回调函数.但是对于大型文件来说,使用C级文件句柄的速度可能会快得多.
没有与下载相关的示例,所以我试过:
library(RCurl)
u = "http://cran.r-project.org/web/packages/RCurl/RCurl.pdf"
f = CFILE("RCurl.pdf", mode="wb")
ret= getURL(u, write = getNativeSymbolInfo("R_curl_write_binary_data")$address,
file = f@ref)
Run Code Online (Sandbox Code Playgroud)
我也尝试用替换file选项writedata = f@ref.该文件已下载但已损坏.为write参数编写自定义回调仅适用于非二进制数据.
有没有想过在RCurl中将二进制文件直接下载到磁盘(不加载到内存中)?
我正在为学校开展一个项目,我需要收集个人NCAA足球运动员的职业统计数据.每个玩家的数据都采用这种格式.
http://www.sports-reference.com/cfb/players/ryan-aplin-1.html
我找不到所有玩家的总和,所以我需要逐页进行并拉出每个传球得分冲击和接收等html表的底行
每个玩家都按照姓氏进行归类,并在此处输入每个字母的链接.
http://www.sports-reference.com/cfb/players/
例如,在这里找到姓氏为A的每个玩家.
http://www.sports-reference.com/cfb/players/a-index.html
这是我第一次真正进入数据抓取,所以我试着用答案找到类似的问题.我找到的最接近的答案是这个问题
我相信我可以使用非常相似的东西,我用收集的玩家的名字切换页码.但是,我不确定如何更改它以查找播放器名称而不是页码.
塞缪尔·文图拉(Samuel L. Ventura)最近也发表了关于NFL数据数据搜集的讨论,可以在这里找到.
编辑:
Ben真的很有帮助,并提供了一些很棒的代码.第一部分非常有效,但是当我尝试运行第二部分时,我遇到了这个问题.
> # unlist into a single character vector
> links <- unlist(links)
> # Go to each URL in the list and scrape all the data from the tables
> # this will take some time... don't interrupt it!
> all_tables <- lapply(links, readHTMLTable, stringsAsFactors = FALSE)
Error in UseMethod("xmlNamespaceDefinitions") :
no applicable method for 'xmlNamespaceDefinitions' applied to an object of class "NULL" …Run Code Online (Sandbox Code Playgroud) 我对R编程比较陌生,我试图将我在Johns Hopkins数据科学专业中学到的一些东西用于实际应用.具体来说,我想自动化从美国财政部网站下载历史债券价格的过程
使用Firefox和R,我能够确定美国财政部网站使用非常简单的HTML POST表单来指定感兴趣的报价的单个日期.然后返回所有未偿债券的二级市场信息表.
我没有尝试使用两个不同的R包来向美国财政部Web服务器提交请求.野兔是我尝试过的两种方法:
尝试#1(使用RCurl):
url <- "https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate.htm"
td.html <- postForm(url,
submit = "Show Prices",
priceDate.year = 2014,
priceDate.month = 12,
priceDate.day = 15,
.opts = curlOptions(ssl.verifypeer = FALSE))
Run Code Online (Sandbox Code Playgroud)
这导致返回并存储的网页,td.html但它包含的是来自treasurydirect服务器的错误消息.我知道服务器正在运行,因为当我通过浏览器提交相同的请求时,我得到了预期的结果.
尝试#2(使用rvest):
s <- html_session(url)
f0 <- html_form(s)
f1 <- set_values(f0[[2]], priceDate.year=2014, priceDate.month=12, priceDate.day=15)
test <- submit_form(s, f1)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这种方法甚至不会留下R并导致来自R的以下错误消息:
Submitting with 'submit'
Error in function (type, msg, asError = TRUE) : <url> malformed
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚如何查看正在发送给rvest的"格式错误"的文本,以便我可以尝试诊断问题.
任何建议或提示解决这个看似简单的任务将不胜感激!