如何填写在线表格并在R中返回结果 [英] How to fill in an online form and get results back in R

查看:89
本文介绍了如何填写在线表格并在R中返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人从R远程填写过Web表单吗?

Has anyone ever filled in a web form remotely from R?

我想用我的成绩在R中做一些射箭统计.有一个非常方便的网页,它为您提供了分类和缺点 http://www.archersmate.co.uk/,我自然希望将其包含在统计信息表中.

I'd like to do some archery statistics in R using my scores. There is a very handy webpage, that gives you the classification and handicaps http://www.archersmate.co.uk/, which I naturally would want to include in my stats sheet.

是否可以远程填写此表格并将结果返回给R ???

Is it possible to fill this form in remotely and to get the results back to R???

否则,我将必须获取所有让分盘并将其自己粘贴到数据库中.

Otherwise I would have to get all handicap tables and stick them into a database myself.

更新: 我们已将问题缩小到以下事实:表单提交按钮是用javascript编写的.

UPDATE: We've narrowed the problem down to the fact, that the form submit button is written in javascript.

推荐答案

您可以使用RSelenium程序包填写和提交Web表单并检索结果.

You can use the RSelenium package to fill out and submit web forms and to retrieve the results.

以下利用 RSelenium 的代码将下载示例输入数据(男性,18岁以下,长弓,布里斯托五世(500):

The following code leveraging RSelenium will download data for an example input (Male, Under 18, Longbow, Bristol V, 500):

library(RSelenium)

# Start Selenium Server --------------------------------------------------------

checkForServer()
startServer()
remDrv <- remoteDriver()
remDrv$open()


# Simulate browser session and fill out form -----------------------------------

remDrv$navigate('http://www.archersmate.co.uk/')
remDrv$findElement(using = "xpath", "//input[@value = 'Male']")$clickElement()
Sys.sleep(2) 
remDrv$findElement(using = "xpath", "//select[@id = 'drpAge']/option[@value = 'Under 18']")$clickElement()
remDrv$findElement(using = "xpath", "//input[@value ='Longbow']")$clickElement() 
remDrv$findElement(using = "xpath", "//select[@id = 'rnd']/option[@value = 'Bristol V']")$clickElement()
remDrv$findElement(using = "xpath", "//input[@id ='scr']")$sendKeysToElement(list('5', '0', '0'))
remDrv$findElement(using = "xpath", "//input[@id = 'cmdCalc']")$clickElement()

# Retrieve and download results injecting javascript ---------------------------

Sys.sleep(2)
clsf <- remDrv$executeScript(script = 'return $("#txtClass").val();', args = list())[[1]]
hndcp <- remDrv$executeScript(script = 'return $("#txtHandicap").val();', args = list())[[1]]

remDrv$quit()
remDrv$closeServer()

RSelenium的默认浏览器是Firefox.但是,RSelenium甚至支持使用PhantomJS进行无头浏览.要利用PhanomJS,您只需

The default browser for RSelenium is Firefox. However, RSelenium even supports headless browsing using PhantomJS. For leveraging PhanomJS you just need to

  • 下载PhantomJS 并将其放置在用户路径中
  • 像下面所述,在代码开头和结尾处替换代码段
  • download PhantomJS and place it in the users path
  • replace the code snippets at the beginning and at the end like described next

默认浏览(如上图所示):

Default browsing (like shown above):

checkForServer()
startServer()
remDrv <- remoteDriver()

...

remDrv$quit()
remDrv$closeServer()

无头浏览:

pJS <- phantom()
remDrv <- remoteDriver(browserName = 'phantomjs')

...

remDrv$close()
pJS$stop()

这篇关于如何填写在线表格并在R中返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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