【问题标题】:Download.File Issue with xls Excel Workbookxls Excel 工作簿的 Download.File 问题
【发布时间】:2018-04-13 07:09:54
【问题描述】:

我正在尝试使用 R 的 download.file 函数 (Windows 10, R version 3.4.4 (2018-03-15)) 下载 Excel 工作簿 xls

当我手动(使用 Internet Explorer 或 Chrome)下载文件时,文件会下载,然后我可以在 Excel 中毫无问题地打开它。

当我在 R 中使用 download.file 时,文件会下载但大小小于正确的下载文件 - 该文件是 hmtl 文件,并带有一些说明我的浏览器不受支持的注释。累了不同的模式,没有运气。

我的代码:

download.file(
        url = "https://www.atsenergo.ru/nreport?fid=696C3DB7A3F6019EE053AC103C8C8733",
        destfile = "C:/MyExcel.xls",
        mode = "wb",
        method = "auto"
      )

【问题讨论】:

    标签: r download xls


    【解决方案1】:

    用 RSelenium 库解决这个问题。 ATS 站点拒绝任何下载文件的查询(返回 .hmtl 文件并带有必需的 javascript 启用消息),在这种情况下 Selenium 方法仅适用。我的代码如下(urlList 带有文件下载链接的数据框):

    rD <- rsDriver(port = 4444L,
                   browser = "chrome",
                   check = FALSE,
                   geckover = NULL,
                   iedrver = NULL,
                   phantomver = NULL)
    remDr <- rD$client
    for (i in 1:nrow(urlList)) {
      tryCatch({
        row <- urlList[i,]
    
        remDr$navigate(row$url)
    
        webElem <-
          remDr$findElement(using =
                              'link text', row$FileName)
        webElem$clickElement()
      },
      error = function(e)
        logerror(paste(
          substr(e, 1, 50),
          atsCode,
          dateFileName,
          sep = "\t"
        ), logger = loggerName),
      finally = next)
    }
    
    remDr$close()
    # stop the selenium server
    rD[["server"]]$stop()
    

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-03
      • 2015-06-06
      相关资源
      最近更新 更多