标签: httr

如何在*服务器身份验证后使用RCurl*下载大型二进制文件

我最初问这个关于使用httr包执行此任务的问题,但我认为不可能使用httr.所以我重新编写了我的代码来RCurl代替使用- 但我仍然踩着可能与...相关的东西writefunction但我真的不明白为什么.

您应该能够使用32位版本的R来重现我的工作,因此如果您在RAM中读取任何内容,则会达到内存限制.我需要一个直接下载到硬盘的解决方案.

首先,这段代码可以正常工作 - 压缩文件被妥善保存到磁盘上.

library(RCurl)
filename <- tempfile()
f <- CFILE(filename, "wb")
url <- "http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip"
curlPerform(url = url, writedata = f@ref)
close(f)
# 2.1 GB file successfully written to disk
Run Code Online (Sandbox Code Playgroud)

现在这里有一些RCurl不起作用的代码.如前一个问题所述,复制这一点将需要在ipums上创建一个提取.

your.email <- "email@address.com"
your.password <- "password"
extract.path <- "https://usa.ipums.org/usa-action/downloads/extract_files/some_file.csv.gz"

library(RCurl)

values <- 
    list(
        "login[email]" = your.email , 
        "login[password]" = your.password , 
        "login[is_for_login]" = 1
    )

curl = getCurlHandle() …
Run Code Online (Sandbox Code Playgroud)

r web-scraping rcurl httr

9
推荐指数
1
解决办法
2053
查看次数

如何正确使用请求标头与API数据请求?

我正试图找到使用httr包连接到Appannie的API的方法(根本没有API连接的经验).API要求在appannie的网站上包含请求标题Citation: 注册App Annie帐户并生成API密钥.将此密钥添加到您的请求标头中,如下所示:
授权:Bearer''
引用

我编写了看起来像这样的代码

query <- "http://api.appannie.com/v1/accounts/1000/sales?break_down=application+dat
&start_date=2012-01-01
&end_date=2012-02-01
&currency=USD
&countries=US
&page_index=1"
getdata<-GET(url=query, add_headers("Authorization: bearer 811b..."))
Run Code Online (Sandbox Code Playgroud)

命令http_status(getdata)显示"客户端错误:(401)未经授权"有人可以帮助我,我做错了什么?

api r httr

8
推荐指数
1
解决办法
7310
查看次数

httr:用POST()检索数据

免责声明:虽然我已经设法使用httr的POST函数从其他来源获取数据,但让我们知道我对httr和HTML表格一般都是完整的n00b.

我想使用httr从网站直接将一些数据带入R. 我的第一次尝试涉及将命名列表传递给body arg(如此插图中所示).但是,我注意到表单输入名称中的方括号(至少我认为它们是表单输入参数).所以相反,我尝试将字体作为字符串传递,因为我认为它应该出现在请求正文中:

url <- 'http://research.stlouisfed.org/fred2/series/TOTALSA/downloaddata'
query <- paste('form[native_frequency]=Monthly', 'form[units]=lin',
                'form[frequency]=Monthly', 'form[obs_start_date]="1976-01-01"',
                'form[obs_end_date]="2014-11-01"', 'form[file_format]=txt'
                sep = '&')
response <- POST(url, body = query)
Run Code Online (Sandbox Code Playgroud)

在任何情况下,上面的代码只返回网页源代码,我无法弄清楚如何正确提交表单,以便它返回与手动单击表单的"下载数据"按钮相同的数据.

在Chrome上的开发人员工具/网络中,它在Content-Disposition下的响应标题中指出,当我手动单击表单上的"下载数据"按钮时,会有一个包含数据的文本文件附件.它似乎不在上面代码中与响应对象关联的任何标头中.为什么POST请求不返回此文件 - 数据的文件在哪里?

感觉就像我错过了一些明显的东西.有人在乎帮助我连接点吗?

html forms r http-post httr

8
推荐指数
1
解决办法
5523
查看次数

Rcurl:当url存在时,url.exists返回false

尝试从特定网页下载信息,虽然它在任何浏览器中都可以正常使用,但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)

r rcurl httr

8
推荐指数
1
解决办法
2836
查看次数

使用R接受cookie以下载PDF文件

我在尝试下载PDF时遇到了问题.

例如,如果我在考古数据服务上有PDF文档的DOI,它将解析到此着陆页, 其中包含嵌入链接到此pdf,但它真正重定向到此其他链接.

library(httr)将处理解析DOI,我们可以使用登陆页面提取PDF格式的URL,library(XML)但我一直坚持获取PDF本身.

如果我这样做:

download.file("http://archaeologydataservice.ac.uk/archiveDS/archiveDownload?t=arch-1352-1/dissemination/pdf/Dyfed/GL44004.pdf", destfile = "tmp.pdf")
Run Code Online (Sandbox Code Playgroud)

然后我收到一个与http://archaeologydataservice.ac.uk/myads/相同的HTML文件

尝试使用R如何从需要cookie的SSL页面下载压缩文件的答案引导我:

library(httr)

terms <- "http://archaeologydataservice.ac.uk/myads/copyrights"
download <- "http://archaeologydataservice.ac.uk/archiveDS/archiveDownload"
values <- list(agree = "yes", t = "arch-1352-1/dissemination/pdf/Dyfed/GL44004.pdf")

# Accept the terms on the form,
# generating the appropriate cookies

POST(terms, body = values)
GET(download, query = values)

# Actually download the file (this will take a while)

resp <- GET(download, query = values)

# write the content …
Run Code Online (Sandbox Code Playgroud)

curl r web-scraping httr

8
推荐指数
1
解决办法
1214
查看次数

在 R 中发出 GET 请求

我一直在使用 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)

特别是,我在传递授权选项时遇到了一些麻烦,因为我无法在两个库中找到等效的参数。这可能是一个自定义标头?

r http rcurl httr

8
推荐指数
2
解决办法
2万
查看次数

在R中以粗体标识网络链接

以下脚本允许我访问具有多个具有相似名称的链接的网站.我想只得到其中一个,因为它在网站上以粗体显示,可以与其他人区别开来.但是,我找不到在列表中选择粗体链接的方法.

有人会对此有所了解吗?提前致谢!

library(httr)
library(rvest)
sp="Alnus japonica"

res <- httr::POST(url ="http://apps.kew.org/wcsp/advsearch.do", 
              body = list(page ="advancedSearch", 
                          AttachmentExist ="", 
                          family ="", 
                          placeOfPub ="", 
                          genus = unlist(strsplit(as.character(sp), split="         "))[1], 
                          yearPublished ="", 
                          species = unlist(strsplit(as.character(sp), split="    "))[2], 
                          author ="", 
                          infraRank ="", 
                          infraEpithet ="", 
                          selectedLevel ="cont"), 
              encode ="form") 
pg <- content(res, as="parsed") 
lnks <- html_attr(html_nodes(pg,"a"),"href")
#how get the url of the link wth accepted name (in bold)?
res2 <- try(GET(sprintf("http://apps.kew.org%s", lnks[grep("id=",lnks)]      [1])),silent=T)
#this gets a link but often fails to get the bold one
Run Code Online (Sandbox Code Playgroud)

html r httr rvest

8
推荐指数
2
解决办法
256
查看次数

curl :: curl_fetch_memory(url,handle = handle)出错:从对等端接收数据失败

我正试图从图表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)

loops r rcurl httr

8
推荐指数
1
解决办法
3553
查看次数

使用带有g-recaptcha-response参数的POST提交表单

我想从以下网页提交表格:http://www.hzzo-net.hr/statos_OIB.htm

首先,我使用2captcha服务绕过recaptcha:

# parameters
api_key <- "c+++"
api_url <- "http://2captcha.com/in.php"
site_key <- "6Lc3SAgUAAAAALFnYxUbXlcJ8I9grvAPC6LFTKQs"
hzzo_url <- "http://www.hzzo-net.hr/statos_OIB.htm"

# GET method
req_url <- paste0("http://2captcha.com/in.php?key=", api_key,"&method=userrecaptcha&googlekey=", 
                  site_key, "&pageurl=", hzzo_url)
get_response <- POST(req_url)
hzzo_content <- content(get_response)
hzzo_content <- xml_text(hzzo_content)
captcha_id <- stringr::str_extract_all(hzzo_content[[1]], "\\d+")[[1]]

# solve captcha
Sys.sleep(16L)
captcha2_solve <- function(apiKey, capstchaID){
  req_url <- paste0("http://2captcha.com/res.php?key=", api_key,"&action=get&id=", capstchaID)
  result <- GET(req_url)
  captcha_content <- content(result)
  hzzo_response <- xml_text(captcha_content)
  hzzo_response <- strsplit(hzzo_response, "\\|")
  return(hzzo_response)
  # hzzo_response <- hzzo_response[[1]][[2]]
  # return(hzzo_response)
}
hzzo_response <- captcha2_solve(api_key, captcha_id) …
Run Code Online (Sandbox Code Playgroud)

python r recaptcha httr 2captcha

8
推荐指数
1
解决办法
1272
查看次数

对 JSON REST API 进行身份验证时如何获取会话令牌(在 R 中)

我正在尝试从 REST API访问 JSON 数据(在R 中)。

为了验证自己的身份,我需要在https://dashboard.server.eu/login 中使用 POST 方法。需要发送的数据是邮箱和密码:

library(httr)

login <- list(
  email = "my@email.com",
  password = "mypass"
)

res <- POST("https://dashboard.server.eu/login", body = login, encode = "form", verbose())
Run Code Online (Sandbox Code Playgroud)

执行上述操作时,我得到以下输出:

-> POST /login HTTP/1.1
-> Host: dashboard.server.eu
-> User-Agent: libcurl/7.59.0 r-curl/3.3 httr/1.4.1
-> Accept-Encoding: gzip, deflate
-> Cookie: session=10kq9qv1udf0107F4C70RY14fsum41sq50
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Content-Length: 53
-> 
>> email=my%40email.com&password=mypass

<- HTTP/1.1 200 OK
<- access-control-allow-headers: Accept, Authorization, Content-Type, If-None-Match …
Run Code Online (Sandbox Code Playgroud)

authentication session r token httr

8
推荐指数
1
解决办法
919
查看次数

标签 统计

httr ×10

r ×10

rcurl ×4

html ×2

web-scraping ×2

2captcha ×1

api ×1

authentication ×1

curl ×1

forms ×1

http ×1

http-post ×1

loops ×1

python ×1

recaptcha ×1

rvest ×1

session ×1

token ×1