【问题标题】:Get table from one frame HTML using Rcurl and XML使用 Rcurl 和 XML 从一帧 HTML 中获取表格
【发布时间】:2014-04-04 21:05:17
【问题描述】:

我如何获得一张通常来自http://portal.inep.gov.br/basica-censo-escolar-matricula 搜索的表格

表格在第一个框架内。

我必须选择拥有数据的州:例如 ACRE 并点击“顾问”

我该怎么做?

【问题讨论】:

    标签: html r web-scraping rcurl


    【解决方案1】:

    您可以通过Selenium 做到这一点

    require(RSelenium)
    appURL <- "http://portal.inep.gov.br/basica-censo-escolar-matricula"
    RSelenium::startServer()
    remDr <- remoteDriver()
    remDr$open()
    remDr$navigate(appURL)
    
    # find iframes
    iframes <- remDr$findElements("css selector", "iframe")
    iframes[[1]]$highlightElement() # visual check
    remDr$switchToFrame(iframes[[1]])
    
    # get Estado selections
    webElems <- remDr$findElements("css selector", "#uf option")
    estadoNames <- sapply(webElems, function(x){x$getElementText()[[1]]})
    webElem <- webElems[[which(estadoNames == "ACRE")]]
    webElem$clickElement()
    
    # click the submit button
    webElem <- remDr$findElement("id", "btnSubmit")
    webElem$clickElement()
    
    # find the table
    webElem <- remDr$findElement("css selector",".Resultado")
    webElem$highlightElement() # visual confirmation
    tableHTML <- webElem$getElementAttribute("outerHTML")[[1]]
    
    remDr$close()
    remDr$closeServer()
    

    【讨论】:

      【解决方案2】:

      那个特定的很棘手。

      当我右键单击 IFRAME 部分并在 Chrome 中执行检查元素时,它看起来像是以一种难以以编程方式获取的方式生成的。

      我可以通过右键单击&lt;html xmlns="http://… 行来提取表格,选择“编辑为 html”,然后进行全选,复制并粘贴到 Sublime Text 中并保存为 HTML 文件(例如:http://rud.is/dl/22872999.html )。

      你可以在 Firefox 中做同样的事情(更简单的 IMO),方法是右键单击表格框架并右键单击/查看源代码,然后将该 HTML 复制并粘贴到文本编辑器(如 Sublime Text)中并保存到 HTML 文件中。

      您应该能够使用标准 R 表/XML 提取代码对以这种方式保存的文件进行操作。

      这远非最佳解决方案,但它确实有效。我希望其他人有更程序化的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 2012-03-19
        • 2011-09-19
        • 2014-05-12
        相关资源
        最近更新 更多