【问题标题】:R RSelenium and phantom in a loopR RSelenium 和幻象在一个循环中
【发布时间】:2015-04-16 00:33:43
【问题描述】:

我有以下代码,我从之前的 Stackoverflow 讨论 (Extracting data from javascript with R) 中借来的。 我基本上是在尝试为某些药物收集一些数据。当我运行单个药品代码 (2203) 的代码时,它工作得很好!

appURL <- "http://web.sivicos.gov.co:8080/consultas/consultas/consreg_encabcum.jsp"
library(RSelenium)
pJS <- phantom(extras = c('--ssl-protocol=tlsv1'))
Sys.sleep(5) # give the binary a moment
remDr <- remoteDriver(browserName = "phantom")
remDr$open()
Sys.sleep(1) # give the binary a moment
remDr$navigate(appURL)
# Get the third list item of the select box (MEDICAMENTOS)
webElem <- remDr$findElement("css", "select[name='grupo'] option:nth-child(3)")
webElem$clickElement() # select this element
# Send text to input value="" name="expediente
webElem <- remDr$findElement("css", "input[name='expediente']")
webElem$sendKeysToElement(list(2203))
# Click the Buscar button
remDr$findElement("id", "INPUT2")$clickElement()
Sys.sleep(3) # give the binary a moment
remDr$switchToFrame(remDr$findElement("css", "iframe[name='datos']"))
remDr$findElement("css", "a")$clickElement() # click the link given in the iframe

# get the resulting data

appData <- remDr$getPageSource()[[1]]
# close phantom js
pJS$stop()

但是当我把它放在一个循环中,这样我就可以检索我需要的所有药物的信息......它就坏了。下面是循环内的代码。

for(cum in 2203){
appURL <- "http://web.sivicos.gov.co:8080/consultas/consultas/consreg_encabcum.jsp"
library(RSelenium)
pJS <- phantom(extras = c('--ssl-protocol=tlsv1'))
Sys.sleep(5) # give the binary a moment
remDr <- remoteDriver(browserName = "phantom")
remDr$open()
Sys.sleep(1) # give the binary a moment
remDr$navigate(appURL)
# Get the third list item of the select box (MEDICAMENTOS)
webElem <- remDr$findElement("css", "select[name='grupo'] option:nth-child(3)")
webElem$clickElement() # select this element
# Send text to input value="" name="expediente
webElem <- remDr$findElement("css", "input[name='expediente']")
webElem$sendKeysToElement(list(cum))
# Click the Buscar button
remDr$findElement("id", "INPUT2")$clickElement()
Sys.sleep(3) # give the binary a moment
remDr$switchToFrame(remDr$findElement("css", "iframe[name='datos']"))
remDr$findElement("css", "a")$clickElement() # click the link given in the iframe

# get the resulting data

appData <- remDr$getPageSource()[[1]]
# close phantom js
pJS$stop()
readHTMLTable(appData, which = 3)

}

关于发生了什么的任何想法?我试着给幻影时间做事,因为我听说这可能是一个问题,但它没有用。它不适用于单个代码 2203 或两个 c(2202,2203)

【问题讨论】:

    标签: r web-scraping phantomjs rselenium


    【解决方案1】:

    该网站现在似乎正在测试用户代理:

    appURL <- "http://web.sivicos.gov.co:8080/consultas/consultas/consreg_encabcum.jsp"
    library(RSelenium)
    pJS <- phantom(extras = c('--ssl-protocol=tlsv1'))
    Sys.sleep(5) # give the binary a moment
    for(cum in "2203"){
      eCap <- list(phantomjs.page.settings.userAgent 
                   = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0")
      remDr <- remoteDriver(browserName = "phantomjs", extraCapabilities = eCap)
      remDr$open()
      Sys.sleep(1) # give the binary a moment
      remDr$navigate(appURL)
      # Get the third list item of the select box (MEDICAMENTOS)
      webElem <- remDr$findElement("css", "select[name='grupo'] option:nth-child(3)")
      webElem$clickElement() # select this element
      # Send text to input value="" name="expediente
      webElem <- remDr$findElement("css", "input[name='expediente']")
      webElem$sendKeysToElement(list(cum))
      # Click the Buscar button
      remDr$findElement("id", "INPUT2")$clickElement()
      Sys.sleep(3) # give the binary a moment
      remDr$phantomExecute("var page = this;
                      page.switchToFrame('datos');
                      page.evaluate(function() {
                        document.querySelector('a').click();
                      });
      ")
    
      # get the resulting data
      Sys.sleep(3) # give the binary a moment
    
      appData <- remDr$getPageSource()[[1]]
      # close phantom js
      readHTMLTable(appData, which = 3) 
      remDr$close()
    }
    pJS$stop()
    

    【讨论】:

    • 感谢您创建这个包...它是一个救生员!我从字面上复制并粘贴了您的代码,但它不起作用。我不确定是不是因为我需要将用户代理设置为特定的东西……我现在正在听你的网络研讨会cran.r-project.org/web/packages/RSelenium/vignettes/…,希望我能学到一些我错过的东西。
    • @MauricioRomero phantomjs2 和 webdriver 在 iframe 中执行 javascript 似乎存在问题。我已经解决了使用phantomExecute 的问题。然而,此时 phantomJS2 在 webdriver 模式下出现了一些错误。您可以切换回 phantom 1.98 或使用其他浏览器(chrome/firefox)。
    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多