有没有人从R远程填写网络表格?
我想使用我的分数在R中做一些射箭统计.有一个非常方便的网页,给你分类和障碍http://www.archersmate.co.uk/,我自然希望包括在我的统计表中.
是否可以远程填写此表格并将结果返回给R ???
否则我将得到所有差点表并将其自己粘贴到数据库中.
更新:
我们已经将问题缩小到了这样一个事实,即表单提交按钮是用javascript编写的.
您可以使用RSelenium包填写并提交Web表单并检索结果.
以下代码利用RSelenium将下载示例输入数据(男性,18岁以下,长弓,布里斯托尔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,你只需要
> download PhantomJS并将其放在用户路径中
>如下所述,在开头和结尾替换代码片段
默认浏览(如上所示):
checkForServer() startServer() remDrv <- remoteDriver() ... remDrv$quit() remDrv$closeServer()
无头浏览:
pJS <- phantom() remDrv <- remoteDriver(browserName = 'phantomjs') ... remDrv$close() pJS$stop()