有没有人从R远程填写过web表单?
我想用我的分数在R中做一些射箭统计。有一个非常方便的网页,给你分类和障碍http://www.archersmate.co.uk/,这是我自然想要包括在我的统计表。
是否可以远程填写此表格并将结果返回R?
否则,我将不得不得到所有的残疾桌子,并把他们自己放在一个数据库。
更新:我们缩小了问题的范围,因为表单提交按钮是用javascript.编写的。
发布于 2014-12-24 00:41:01
您可以使用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的默认浏览器是火狐。然而,RSelenium甚至支持使用PhantomJS进行无头浏览。为了利用PhanomJS,你只需要
默认浏览(如上文所示):
checkForServer()
startServer()
remDrv <- remoteDriver()
...
remDrv$quit()
remDrv$closeServer()无头浏览:
pJS <- phantom()
remDrv <- remoteDriver(browserName = 'phantomjs')
...
remDrv$close()
pJS$stop()发布于 2013-04-30 17:10:31
这可能对您没有帮助,因为我正在寻找类似问题的答案,但是查看您想要抓取的URL,需要填写的表单实际上是所有HTML表单,您可以通过以下方法获得说明:
url <- "http://www.archersmate.co.uk/"
forms <- getHTMLFormDescription(url)还请看omegahat.org上的包“omegahat.org”
发布于 2014-02-25 13:25:16
这不能在RCurl中完成,因为表单会触发ajax事件,所以postForm函数是不够的。
https://stackoverflow.com/questions/14237257
复制相似问题