使用R中的提交按钮将数据发布到Web表单 [英] Posting data to web forms using submit buttons from R

查看:83
本文介绍了使用R中的提交按钮将数据发布到Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从R发布到Web表单,然后检索结果.这有可能吗?

特别是,我想将文本文件传递到此网站 http://ionspectra.org/aristo/batchmode/ 并检索结果.

网站使用的发布方式是

<form action="../batchreport/" method="post" enctype="multipart/form-data"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='d9c49e206913e5d8b515bc9705ca2e09' />

首先,我想检查以制表符分隔的单选按钮格式":

<input type="radio" name="format" value="tsv" /> Tab-delimited <br/>

然后我要上传一个给定的文件:

<input type="file" name="batchfile" size="20"><br/>

然后单击提交按钮:

<input type="submit" value="Ontologize!" />

最后检索得到的文本文件.

问题是,可以用R编写脚本吗?如果可以,请使用什么包? 可以使用RCurlpostForm完成吗?但是,如果是这样,在这种情况下的语法是什么?

欢迎任何建议!

干杯,汤姆

解决方案

由于它是Django网站,因此比正常情况有些棘手,并且我们需要通过生成CSRF令牌来处理Django的跨站点请求伪造保护. /p>

使用此处提供的示例文件,这是使用httr的方法. :

library(httr)
csrf <- GET(url='http://ionspectra.org/aristo/batchmode/')$cookies$csrftoken
res <- POST(url='http://ionspectra.org/aristo/batchreport/', 
            body=list(batchfile=upload_file('example.txt'),
                      format='tsv',
                      csrfmiddlewaretoken=csrf))
out <- read.delim(file=textConnection(content(res)), 
                  stringsAsFactors=FALSE)

GET调用将生成CSRF令牌,这是后续POST调用所必需的.

I would like to post data to a web form from R, and retrieve the result. Is this possible at all?

In particular, I would like to pass a text file to this website http://ionspectra.org/aristo/batchmode/ and retrieve the result.

The post method the website uses is

<form action="../batchreport/" method="post" enctype="multipart/form-data"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='d9c49e206913e5d8b515bc9705ca2e09' />

First I would like to check the radio button "format" to Tab-delimited:

<input type="radio" name="format" value="tsv" /> Tab-delimited <br/>

Then I would like to upload a given file:

<input type="file" name="batchfile" size="20"><br/>

Then have the submit button clicked:

<input type="submit" value="Ontologize!" />

And finally have the resulting text file be retrieved.

Question is, can this be scripted from R, and if so, using what package? Can it be done using RCurl's postForm perhaps? But if so, what would be the syntax in this case?

Any advice welcome!

cheers, Tom

解决方案

This is a little trickier than normal since it's a Django website, and we need to deal with Django's Cross-site Request Forgery protection by generating a CSRF token.

Here's how to do it with httr, using the example file provided here:

library(httr)
csrf <- GET(url='http://ionspectra.org/aristo/batchmode/')$cookies$csrftoken
res <- POST(url='http://ionspectra.org/aristo/batchreport/', 
            body=list(batchfile=upload_file('example.txt'),
                      format='tsv',
                      csrfmiddlewaretoken=csrf))
out <- read.delim(file=textConnection(content(res)), 
                  stringsAsFactors=FALSE)

The GET call generates the CSRF token, which is needed for the subsequent POST call.

这篇关于使用R中的提交按钮将数据发布到Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆