我想使用R包httr通过他们的API访问EC2服务.但我有点不确定如何开始,因为它不属于通常的"Oauth2.0"身份验证格式,其中你有通常的:密钥,秘密,令牌和签名系统.我认为EC2使用"签名版本2"方法,但我不清楚它是如何工作的.
查看EC2提供的有关在http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using-query-api.html上发出查询请求的文档
我想我需要签名的价值......但不知道如何得到它
我已尝试使用一些给定的命令httr,如下所示.我可以适应大部分的URL字符串参数来表示我,我想做例如事情AWSAccessKeyId,ImageId,endpoint和Action等....但就是不知道哪里去获得签名值.
同样在给出的一些例子中,他们似乎也没有提供秘密访问密钥......
所以尝试过的命令如下所示,改变了一些值来代表我,但得到了以下内容:
require(httr)
GET("https://ec2.amazonaws.com/
?Action=RunInstances
&ImageId=ami-60a54009
&MaxCount=3
&MinCount=1
&Placement.AvailabilityZone=us-east-1b
&Monitoring.Enabled=true
&AWSAccessKeyId=0GS7553JW74RRM612K02EXAMPLE
&Version=2012-10-01
&Expires=2010-10-10T12:00:00Z
&Signature=lBP67vCvGlDMBQ1dofZxg8E8SUEXAMPLE
&SignatureVersion=2
&SignatureMethod=HmacSHA256")
Run Code Online (Sandbox Code Playgroud)
我得到了答复:
Response [http://aws.amazon.com/ec2/]
Status: 200
Content-type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" type="image/ico" href="//d36cz9buwru1tt.cloudfront.net/favicon.ico">
<link rel="shortcut icon" type="image/ico" href="//d36cz9buwru1tt.cloudfront.net/favicon.ico">
<meta name="description" content="Amazon Elastic Compute Cloud delivers scalable, pay-as-you-go compute capacity in the cloud. " /><meta …Run Code Online (Sandbox Code Playgroud) 已经制定了OAuth的签名审批制度,对Dropbox的,我想下载,我救了一个有文件.RData使用API,以及httr的GET功能.
该请求是sucessfull与数据回来,但它是在原始格式,并想知道我怎么去再次将其转换成一个RDATA文件我的本地驱动器上.
这就是我到目前为止所做的:......
require(httr)
db.file.name <- "test.RData"
db.app <- oauth_app("db",key="xxxxx", secret="xxxxxxx")
db.sig <- sign_oauth1.0(db.app, token="xxxxxxx", token_secret="xxxxxx")
response <- GET(url=paste0("https://api-content.dropbox.com/1/files/dropbox/",db.file.name),config=c(db.sig,add_headers(Accept="x-dropbox-metadata")))
str(response)
List of 8
$ url : chr "https://api-content.dropbox.com/1/files/dropbox/test.RData"
$ handle :List of 2
..$ handle:Formal class 'CURLHandle' [package "RCurl"] with 1 slots
.. .. ..@ ref:<externalptr>
..$ url :List of 8
.. ..$ scheme : chr "https"
.. ..$ hostname: chr "api-content.dropbox.com"
.. ..$ port : NULL
.. ..$ path : chr ""
.. ..$ query …Run Code Online (Sandbox Code Playgroud) 这是对此的跟进/更新问题:
我正在寻找有关如何将表从DynamoDB读入R的示例或文档.
这个问题指出了我正确的方向:
(伟大的@hadley自己回答!).
如果我必须使用httr然后解析json响应,这是可以的,但我甚至无法弄清楚如何格式化POST请求.
谢谢!
我正在尝试从页面下载内容,并且我发现响应数据格式不正确或不完整,就好像GET或getURL在加载这些数据之前一样.
library(httr)
library(RCurl)
url <- "https://www.vanguardcanada.ca/individual/etfs/etfs.htm"
d1 <- GET(url) # This shows a lot of {{ moustache style }} code that's not filled
d2 <- getURL(url) # This shows "" as if it didn't get anything
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办.我的目标是获取与浏览器中显示的链接相关联的数字:
https://www.vanguardcanada.ca/individual/etfs/etfs-detail-overview.htm?portId=9548
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,我想下载并刮掉'9548'.
不确定为什么getURL和GET似乎与浏览器中显示的结果大相径庭.似乎数据加载缓慢,几乎就像GET和getURL在完全加载之前一样.
例如,看看:
x <- "https://www.vanguardcanada.ca/individual/etfs/etfs-detail-prices.htm?portId=9548"
readHTMLTable(htmlParse(GET(x)))
Run Code Online (Sandbox Code Playgroud) 我使用httr将简单文件上传到Google云端硬盘.问题是每个文档都被上传为"无标题",我必须修补元数据以设置标题.PATCH请求偶尔会失败.
根据API,我应该能够进行分段上传,允许我将标题指定为上传文件的同一POST请求的一部分.
res<-POST(
"https://www.googleapis.com/upload/drive/v2/files?convert=true",
config(token=google_token),
body=list(y=upload_file(file))
)
id<-fromJSON(rawToChar(res$content))$id
if(is.null(id)) stop("Upload failed")
url<-paste(
"https://www.googleapis.com/drive/v2/files/",
id,
sep=""
)
title<-strsplit(basename(file), "\\.")[[1]][1]
Sys.sleep(2)
res<-PATCH(url,
config(token=google_token),
body=paste('{"title": "',title,'"}', sep = ""),
add_headers("Content-Type" = "application/json; charset=UTF-8")
)
stopifnot(res$status_code==200)
cat(id)
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的事情:
res<-POST(
"https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&convert=true",
config(token=google_token),
body=list(y=upload_file(file),
#add_headers("Content-Disposition" = "text/json"),
json=toJSON(data.frame(title))
),
encode="multipart",
add_headers("Content-Type" = "multipart/related"),
verbose()
)
Run Code Online (Sandbox Code Playgroud)
我得到的输出显示各个部分的内容编码是错误的,它导致400错误:
-> POST /upload/drive/v2/files?uploadType=multipart&convert=true HTTP/1.1
-> User-Agent: curl/7.19.7 Rcurl/1.96.0 httr/0.6.1
-> Host: www.googleapis.com
-> Accept-Encoding: gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Authorization: Bearer ya29.ngGLGA9iiOrEFt0ycMkPw7CZq23e6Dgx3Syjt3SXwJaQuH4B6dkDdFXyIC6roij2se7Fs-Ue_A9lfw
-> Content-Length: 371 …Run Code Online (Sandbox Code Playgroud) 我正在努力使用httroauth2.0 功能连接到 Google Analytics
oauth2.0_token(oauth_endpoints("google")
, oauth_app("google", client.id, client.secret)
, scope = "https://www.googleapis.com/auth/analytics.readonly")
Run Code Online (Sandbox Code Playgroud)
它在我本地的 Rstudio 中运行良好,但在基于 AWS 的 Rstudio Server 中会中断。当我同意在浏览器中传递数据并且谷歌将我重定向到页面 http://localhost:1410/?state=codehere 时出现错误
在本地 Rstudio 中启动身份验证时,浏览器响应消息 -身份验证完成。请关闭此页面并返回 R,以防 Rstudio 服务器它只是此网页不可用
我怀疑我需要更改listener_endpoint配置,但是如何更改?我应该输入我的 Rstudio 服务器地址而不是默认的 127.0.0.1 吗?或者是httr+Rtudio服务器的缺陷,我不应该打扰?
我正在测试R中的一些web scrape脚本.我已经阅读了很多教程,文档并尝试了不同的东西,但到目前为止还没有成功.
我试图抓取的URL就是这个.它有公共,政府数据,没有针对网络抓取工具的声明.它是葡萄牙语,但我相信这不会是一个大问题.
它显示了一个包含多个字段的搜索表单.我的测试是搜索来自特定州("RJ",在这种情况下,该字段是"UF")和城市("Rio de Janeiro",在"MUNICIPIO"字段中)的数据.通过单击"Pesquisar"(搜索),它显示以下输出:
使用Firebug,我发现它调用的URL(使用上面的参数)是:
http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam?buscaForm=buscaForm&codEntidadeDecorate%3AcodEntidadeInput=&noEntidadeDecorate%3AnoEntidadeInput=&descEnderecoDecorate%3AdescEnderecoInput=&estadoDecorate%3A**estadoSelect=33**&municipioDecorate%3A**municipioSelect=3304557**&bairroDecorate%3AbairroInput=&pesquisar.x=42&pesquisar.y=16&javax.faces.ViewState=j_id10
Run Code Online (Sandbox Code Playgroud)
该网站使用jsessionid,使用以下内容可以看到:
library(rvest)
library(httr)
url <- GET("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/")
cookies(url)
Run Code Online (Sandbox Code Playgroud)
知道它使用了jsessionid,我使用cookies(url)检查这些信息,并将其用于这样的新URL:
url <- read_html("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam;jsessionid=008142964577DBEC622E6D0C8AF2F034?buscaForm=buscaForm&codEntidadeDecorate%3AcodEntidadeInput=33108064&noEntidadeDecorate%3AnoEntidadeInput=&descEnderecoDecorate%3AdescEnderecoInput=&estadoDecorate%3AestadoSelect=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&bairroDecorate%3AbairroInput=&pesquisar.x=65&pesquisar.y=8&javax.faces.ViewState=j_id2")
html_text(url)
Run Code Online (Sandbox Code Playgroud)
好吧,输出没有数据.实际上,它有一条错误消息.翻译成英文,它基本上说会话已经过期.
我认为这是一个基本的错误,但我四处寻找并找不到克服这个问题的方法.
我正试图用Rvest从$ JOB的内部网页上刮掉一张桌子.我已经使用这里列出的方法来获取xpath等.
我的代码非常简单:
library(httr)
library(rvest)
un = "username"; pw = "password"
thexpath <- "//*[@id="theFormOnThePage"]/fieldset/table"
url1 <- "https://biglonghairyURL.do?blah=yadda"
stuff1 <- read_html(url1, authenticate(un, pw))
Run Code Online (Sandbox Code Playgroud)
这给我一个错误:"对等证书无法使用给定的CA证书进行身份验证."
撇开不向上到datedness证书的,我已经看到,它可能使用HTTR避免SSL验证使用set_config(config(ssl_verifypeer = 0L)).
如果我使用来自httr的GET(url1),这只会很好用,但重点是使用rvest自动抓取表格.
看看Rvest和httr的PDF ,似乎Rvest调用httr传递curl命令,而在httr中,你可以使用config().
那么,要完成三段论,我怎么能(或者甚至可能?)直接通过rvest :: read_html传递ssl_verifypeer = 0L?
我尝试了很多变化:
stuff1 <- read_html(url1, authenticate(un, pw), ssl_verifypeer = 0L))
stuff1 <- read_html(url1, authenticate(un, pw), config(ssl_verifypeer = 0L)))
stuff1 <- with_config(config = config(ssl_verifypeer = 0L), read_html(url1, authenticate(un, pw)))
Run Code Online (Sandbox Code Playgroud)
并且所有这些都抛出相同的错误"对等证书无法使用给定的CA证书进行身份验证".
希望这是可能的,我只是没有把正确的语法放在一起?
有人建议使用RSelenium,但由于这是在受保护的VM中,安装java和/或新软件包需要采取国会行为(以及VP签收),这将是我的最后手段.
我非常感谢任何有关这方面的建议.
我想获得的结果,这种形式用httr.
查看了表单结果后,我尝试了以下方法:
library(httr)
library(stringr)
r = str_c("http://www.memoiredeshommes.sga.defense.gouv.fr/fr/arkotheque/",
"client/mdh/base_morts_pour_la_france_premiere_guerre/index.php")
q = list(
"action" = 1,
"todo" = "rechercher",
"le_id" = "",
"multisite" = "",
"r_c_nom" = "mo",
"r_c_nom_like" = 1,
"r_c_prenom" = "",
"r_c_prenom_like" = 1,
"r_c_naissance_jour_mois_annee_jj_debut" = "",
"r_c_naissance_jour_mois_annee_mm_debut" = "",
"r_c_naissance_jour_mois_annee_yyyy_debut" = 1890,
"r_c_naissance_jour_mois_annee_jj_fin" = "",
"r_c_naissance_jour_mois_annee_mm_fin" = "",
"r_c_naissance_jour_mois_annee_yyyy_fin" = "",
"r_c_id_naissance_departement" = "",
"hidden_c_id_naissance_departement" = "",
"r_c_id_naissance_pays" = "",
"hidden_c_id_naissance_pays" = "",
"r_annot_c_id_grade" = "",
"hidden_c_id_grade" = "",
"r_annot_c_id_unite" = "", …Run Code Online (Sandbox Code Playgroud) 我想从服务器提供的GeoTIFF数据创建一个RasterLayer.我将使用httr :: GET调用向服务器查询此数据(数据是按需提供的,因此在应用程序中不会有以.tif结尾的URL,而是查询URL).
将此调用的结果作为GeoTIFF文件写入磁盘后,很容易从磁盘上生成的GeoTIFF文件创建RasterLayer:
library(httr)
library(raster)
url <- 'http://download.osgeo.org/geotiff/samples/gdal_eg/cea.tif'
geotiff_file <- tempfile(fileext='.tif')
httr::GET(url,httr::write_disk(path=geotiff_file))
my_raster <- raster(geotiff_file)
my_raster
Run Code Online (Sandbox Code Playgroud)
但是,我想跳过写入磁盘部分并直接从内存服务器响应创建栅格.
response <- httr::GET(url,httr::write_memory())
response
Run Code Online (Sandbox Code Playgroud)
响应的内容是一个原始字符串,我需要将其解释为geoTIFF数据.
str(httr::content(response))
Run Code Online (Sandbox Code Playgroud)
但是,我只能找到从文件中读取的raster或rgdal函数.有关将此原始字符串转换为栅格的任何建议吗?
谢谢!
httr ×10
r ×10
curl ×2
rcurl ×2
rvest ×2
amazon-ec2 ×1
dropbox-api ×1
ec2-ami ×1
file-upload ×1
geotiff ×1
get ×1
multipart ×1
raster ×1
rgdal ×1
rstudio ×1
ssl ×1
web-scraping ×1