我有一个手动过程,我通过curl将5-6 GB文件上传到Web服务器:
curl -X POST --data-binary @myfile.csv http://myserver::port/path/to/api
Run Code Online (Sandbox Code Playgroud)
这个过程很好,但是我喜欢用R自动化它.问题是,我要么不知道我在做什么,要么curl的R库不知道如何处理大于~2GB的文件:
library(RCurl)
postForm(
"http://myserver::port/path/to/api",
file = fileUpload(
filename = path.expand("myfile.csv"),
contentType = "text/csv"
),.encoding="utf-8")
Run Code Online (Sandbox Code Playgroud)
Yeilds Error: Internal Server Error
httr也不起作用:
library(httr)
POST(
url = "http://myserver:port/path/to/api",
body = upload_file(
path = path.expand("myfile.csv"),
type = 'text/csv'),
verbose()
)
Run Code Online (Sandbox Code Playgroud)
产量:
Response [http://myserver:port/path/to/api]
Date: 2015-06-30 11:11
Status: 400
Content-Type: <unknown>
<EMPTY BODY>
Run Code Online (Sandbox Code Playgroud)
httr对该verbose()
选项提供了更多信息,告诉我:
-> POST http://myserver:port/path/to/api
-> User-Agent: libcurl/7.35.0 r-curl/0.9 httr/1.0.0
-> Host: http://myserver::port
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */* …
Run Code Online (Sandbox Code Playgroud) 我正在尝试修复我公开提供的下载自动化脚本,以便任何人都可以使用R轻松下载世界价值观调查.
在此网页- http://www.worldvaluessurvey.org/WVSDocumentationWV4.jsp - PDF链接"WVS_2000_Questionnaire_Root"容易在Firefox和chrome.I下载无法弄清楚如何自动下载使用httr
或RCurl
或任何其它的R包.下面是Chrome互联网行为的截图.PDF链接需要跟进http://www.worldvaluessurvey.org/wvsdc/DC00012/F00001316-WVS_2000_Questionnaire_Root.pdf的最终来源,但如果直接点击它们,则会出现连接错误.我不清楚这是否与请求标头Upgrade-Insecure-Requests:1
或响应标头状态代码有关302
点击新的worldvaluessurvey.org网站,使用chrome的inspect元素窗口打开让我觉得这里有一些hacky编码决策,因此标题半破:/
我正在为http://asdfree.com/编写一组可自由下载的R脚本,以帮助人们分析英国数据服务托管的复杂样本调查数据.除了为这些数据集提供大量统计教程之外,我还想自动下载和导入此调查数据.为了做到这一点,我需要弄清楚如何以编程方式登录这个英国数据服务网站.
我已经尝试了许多不同的RCurl和httr配置来登录,但是我在某个地方犯了一个错误而且我被卡住了.我已经尝试检查这篇文章中概述的元素,但网站在浏览器中跳得太快,让我无法理解发生了什么.
这个网站确实需要登录名和密码,但我相信在进入登录页面之前我犯了一个错误.
起始页应为:https://www.esds.ac.uk/secure/UKDSRegister_start.asp
此页面将自动将您的Web浏览器重定向到以以下内容开头的长URL: https://wayf.ukfederation.org.uk/DS002/uk.ds?[blahblahblah]
(1)由于某种原因,SSL证书在本网站上不起作用.这是我发布的有关此问题的SO问题.我使用的解决方法是忽略SSL:
library(httr)
set_config( config( ssl.verifypeer = 0L ) )
Run Code Online (Sandbox Code Playgroud)
然后我在起始网站上的第一个命令是:
z <- GET( "https://www.esds.ac.uk/secure/UKDSRegister_start.asp" )
Run Code Online (Sandbox Code Playgroud)
这给了我一个z$url
看起来很像https://wayf.ukfederation.org.uk/DS002/uk.ds?[blahblahblah]
我的浏览器也重定向的页面.
然后,在浏览器中,您应该输入"英国数据存档"并单击continue
按钮.当我这样做时,它会将我重定向到网页https://shib.data-archive.ac.uk/idp/Authn/UserPassword
我认为这是我被困的地方,因为我无法弄清楚如何followlocation
在这个网站上获得cURL 和土地.注意:尚未输入用户名/密码.
当我使用wayf.ukfederation.org.uk页面中的httr GET
命令时,如下所示:
y <- GET( z$url , query = list( combobox = "https://shib.data-archive.ac.uk/shibboleth-idp" ) )
Run Code Online (Sandbox Code Playgroud)
该y$url
字符串看起来很像z$url
(除非它有一个下拉框=上年底).有没有办法通过RCurl或httr进入此uk data …
我试图从R中受密码保护的网站上抓取数据.看来,httr和RCurl软件包似乎是使用密码认证进行抓取的最佳选择(我也查看了XML包).
我试图抓取的网站如下(您需要一个免费帐户才能访问整个页面):http: //subscribers.footballguys.com/myfbg/myviewprojections.php ?projector = 2
这是我的两次尝试(用我的用户名替换"username",用我的密码替换"password"):
#This returns "Status: 200" without the data from the page:
library(httr)
GET("http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2", authenticate("username", "password"))
#This returns the non-password protected preview (i.e., not the full page):
library(XML)
library(RCurl)
readHTMLTable(getURL("http://subscribers.footballguys.com/myfbg/myviewprojections.php?projector=2", userpwd = "username:password"))
Run Code Online (Sandbox Code Playgroud)
我查看了其他相关帖子(下面的链接),但无法弄清楚如何将他们的答案应用到我的案例中.
如何用R(https链接)webscrape安全页面(使用XML包中的readHTMLTable)?
http://www.inside-r.org/questions/how-scrape-data-password-protected-https-website-using-r-hold
我现在正在使用httr
v0.2包来使用github api.但是我很难超越oauth2.0(...)
我进入应用程序浏览器页面的部分,单击"允许"然后重定向到回调URL页面.
httr github演示建议使用回调URL,http://localhost:1410
但当我被重定向到该页面时,谷歌浏览器表明它无法连接到该页面,而它正在被重定向的页面是http://localhost:1410/?error=redirect_uri_mismatch&state=DZNFcm8tnq
...所以我尝试了一堆其他端口和整体URL没有成功......
什么是另一个回调URL和URL可以工作?
下面是我使用的代码
require(httr)
## Loading required package: httr
github.app <- oauth_app("github","xxxxx", "xxxxxxxxxxxxxxx")
github.urls <- oauth_endpoint(NULL, "authorize", "access_token",base_url = "https://github.com/login/oauth")
github.token <- oauth2.0_token(github.urls,github.app)
## Loading required package: Rook
## Loading required package: tools
## Loading required package: brew
## starting httpd help server ... done
## Waiting for authentication in browser...
Run Code Online (Sandbox Code Playgroud)
这是当我被定向到一个页面,其中有一个'允许'按钮,点击之后,我被重定向到google chrome中无法连接到localhost的页面:1410
当通过POST请求发送服务器数据时,我无法弄清楚如何模仿浏览器的功能.以下是相关网址,下面有解释.
(1) http://kenpom.com/
(2) http://kenpom.com/register.php?frompage=1
<form id="login" method="POST" action="handlers/login_handler.php">
<label>E-mail </label><input type="text" name="email" />
<label>Password </label><input type="password" name="password" />
<input type="submit" name="submit" value="Login!" />
(3) http://kenpom.com/team.php?team=Rice
Run Code Online (Sandbox Code Playgroud)
(1)主页(未登录时选择团队页面,重定向 - >(2))
(2)登录页面(成功登录后重定向到团队特定页面)
(3)团队专用页面:例如Rice
url <- ("http://kenpom.com/team.php?team=Rice")
login <- list(
email = "login",
password = "password"
)
teampage <- POST(url, body = login)
Response [http://kenpom.com/register.php?frompage=1]
Date: 2015-03-07 23:04
Status: 200
Content-Type: text/html
Size: 7.45 kB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<LINK REL=stylesheet TYPE="text/css" HREF="css/rate.css?1414365416">
<TITLE>kenpom.com subscription</TITLE>
Run Code Online (Sandbox Code Playgroud)
最终想要使用rvest软件包来获取一些信息,但最终会因为它试图刮掉而得到空洞的结果:http …
我正在尝试查询此网站以获取优惠列表.问题是我们需要在收到最终结果之前填写2个表单(2个POST查询).
这就是我到目前为止所做的:
首先,我在设置cookie后发送第一个POST:
library(httr)
set_cookies(.cookies = c(a = "1", b = "2"))
first_url <- "https://compare.switchon.vic.gov.au/submit"
body <- list(energy_category="electricity",
location="home",
"location-home"="shift",
"retailer-company"="",
postcode="3000",
distributor=7,
zone=1,
energy_concession=0,
"file-provider"="",
solar=0,
solar_feedin_tariff="",
disclaimer_chkbox="disclaimer_selected")
qr<- POST(first_url,
encode="form",
body=body)
Run Code Online (Sandbox Code Playgroud)
然后尝试使用第二个帖子查询检索商品:
gov_url <- "https://compare.switchon.vic.gov.au/energy_questionnaire/submit"
qr1<- POST(gov_url,
encode="form",
body=list(`person-count`=1,
`room-count`=1,
`refrigerator-count`=1,
`gas-type`=4,
`pool-heating`=0,
spaceheating="none",
spacecooling="none",
`cloth-dryer`=0,
waterheating="other"),
set_cookies(a = 1, b = 2))
)
library(XML)
dc <- htmlParse(qr1)
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我收到一条消息,表明会话结束.非常感谢您解决此问题的任何帮助.
我添加了cookie和中间GET,但我仍然没有任何结果.
library(httr)
first_url <- "https://compare.switchon.vic.gov.au/submit"
body <- list(energy_category="electricity",
location="home",
"location-home"="shift",
"retailer-company"="",
postcode=3000,
distributor=7,
zone=1,
energy_concession=0,
"file-provider"="", …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用R 自动下载此网页上可用的收入动态面板研究文件.点击任何这些文件会将用户带到此登录/身份验证页面.身份验证后,可以使用Web浏览器轻松下载文件.不幸的是,下面的代码似乎没有维护身份验证.我已经尝试在Chrome中检查Login.aspx页面(如此处所述),但即使我认为我传递了所有正确的值,它似乎也不会保持身份验证.如果是与做我不关心或者还是其他什么东西,我只是喜欢的事,在'r的作品,所以我不需要有此脚本的用户必须手动或使用一些完全独立的程序下载的文件.我对此的尝试之一如下,但它不起作用.任何帮助,将不胜感激.谢谢!!:dhttr
Headers
httr
RCurl
require(httr)
values <-
list(
"ctl00$ContentPlaceHolder3$Login1$UserName" = "you@email.com" ,
"ctl00$ContentPlaceHolder3$Login1$Password" = "somepassword" ,
"ctl00$ContentPlaceHolder3$Login1$LoginButton" = "Log In" ,
"_LASTFOCUS" = "" ,
"_EVENTTARGET" = "" ,
"_EVENTARGUMENT" = ""
)
POST( "http://simba.isr.umich.edu/u/Login.aspx?redir=http%3a%2f%2fsimba.isr.umich.edu%2fZips%2fZipMain.aspx" , body = values )
resp <- GET( "http://simba.isr.umich.edu/Zips/GetFile.aspx" , query = list( file = "1053" ) )
Run Code Online (Sandbox Code Playgroud) 有谁知道如何使用R制定以下SOAP请求?
POST /API/v201010/AdvertiserService.asmx HTTP/1.1
Host: advertising.criteo.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://advertising.criteo.com/API/v201010/clientLogin"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<clientLogin xmlns="https://advertising.criteo.com/API/v201010">
<username>string</username>
<password>string</password>
<source>string</source>
</clientLogin>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud) 我有一个大约13,000个URL的列表,我想从中提取信息,但是,并非每个URL实际存在.事实上,大多数人没有.我刚尝试通过所有13,000个网址,html()
但需要很长时间.我试图找出如何在解析它们之前查看url是否实际存在html()
.我已经尝试使用httr
和GET()
功能,以及rcurls
和url.exists()
功能.由于某种原因,即使URL确实存在,也url.exist()
始终返回FALSE
值,并且我使用的方式GET()
总是返回成功,我认为这是因为页面被重定向.
以下URL表示我正在解析的页面类型,第一个不存在
urls <- data.frame('site' = 1:3, 'urls' = c('https://www.deakin.edu.au/current-students/unitguides/UnitGuide.php?year=2015&semester=TRI-1&unit=SLE010',
'https://www.deakin.edu.au/current-students/unitguides/UnitGuide.php?year=2015&semester=TRI-2&unit=HMM202',
'https://www.deakin.edu.au/current-students/unitguides/UnitGuide.php?year=2015&semester=TRI-2&unit=SLE339'))
urls$urls <- as.character(urls$urls)
Run Code Online (Sandbox Code Playgroud)
因为GET()
,问题是第二个URL实际上并不存在,但它被重定向,因此返回"成功".
urls$urlExists <- sapply(1:length(urls[,1]),
function(x) ifelse(http_status(GET(urls[x, 'urls']))[[1]] == "success", 1, 0))
Run Code Online (Sandbox Code Playgroud)
因为url.exists()
,即使第一个和第三个URL确实存在,我也会返回三个FALSE.
urls$urlExists2 <- sapply(1:length(urls[,1]), function(x) url.exists(urls[x, 'urls']))
Run Code Online (Sandbox Code Playgroud)
我查了一下这两个职位1,2,但我宁愿不使用的用户代理,只是因为我不知道如何找到我的,或者它是否会使用其他计算机上的验证码不同人的变化.因此,使代码更难以被其他人接收和使用.两篇帖子的答案建议使用GET()
in httr
.这似乎GET()
是首选的方法,但我需要弄清楚如何处理重定向问题.
在解析它们之前,任何人都可以建议在R中测试URL的存在html()
吗?我也很乐意为此问题提出任何其他建议的工作.
更新:
在查看了返回的值后,GET()
我想出了一个解决方法,详细了解答案.