【发布时间】:2014-11-24 16:17:10
【问题描述】:
我相当精通 R,但对 javaScript 和其他语言一无所知。我想访问有关此公开数据集 (http://fyed.elections.on.ca/fyed/en/form_page_en.jsp) 的信息。特别是,我在一个数据框中有一个包含数千个 ('A1A1A1') 形式的邮政编码的列表。我想将这些邮政编码中的每一个提交到该网站,然后提取返回的选区名称。 RSelenium 似乎很理想,但我无法弄清楚如何让 javascript 工作。 我正在使用 R 3.0.3 和 RSelenium_1.3 开发 Mac OS 10.9.5。 Firefox 是 v. 33,Selenium 是 2.44。以下脚本有效。
require(RSelenium)
checkForServer()
startServer()
remDr<-remoteDriver()
remDr$open()
remDr$getStatus()
remDr$navigate("http://fyed.elections.on.ca/fyed/en/form_page_en.jsp")
#After inspecting the source code, you can see the input box has the id 'pcode', for postal code
webElem<-remDr$findElement(using = 'id', value = "pcode")
webElem$getElementAttribute('id')
#This is where I am stuck
remDr$executeScript(script='arguments[0].click(m1p4v4)', list(webElem))
#Utlimately, I have a list of several thousand postal codes, so I would like to create a loop through to extract all the district names that are stored on the pages that are returned with a successful javascript (see previous command). Three real postal codes that return results are as follows:
p.codes<-c('m1p4v4', 'n3t2y3', 'n2h3v1')
我觉得我只是不了解使这项工作所需的 javascript 命令或 executeScript 的语法。如有任何帮助,我将不胜感激。
【问题讨论】:
标签: javascript r selenium web-scraping