簡體   English   中英

如何通過rselenium下載文件

[英]how to download files via rselenium

我正在嘗試通過 Rselium 下載文件,但已經 4 天沒有成功。 我想要做的就是讓 Rselium 單擊下載鏈接並將其保存到磁盤上的本地文件中。

我在 windows 10 機器上使用 Docker。

在本頁https://www.rba.gov.au/mkt-operations/resources/tech-notes/eligible-securities.html

我要下載List of eligible securities

根據下面@Nad pat 的反饋,我現在更新了我的代碼。 我仍然無法將文件下載到指定目錄!

更新代碼

library(tidyverse)
library(lubridate)
library(XML)
library(readxl)
library(janitor)    

library(RSelenium)


fprof <- makeFirefoxProfile(list(
    
    browser.download.manager.showWhenStarting = FALSE,
    
    browser.download.dir = str_replace_all("c:/users/joe/downloads/", "/", "\\\\\\\\"),
    # browser.download.dir = "c:/users/joe/downloads",
    # browser.download.useDownloadDir = str_replace_all("c:/users/joe/downloads/", "/", "\\\\\\\\"),

    
    rowser.helperApps.neverAsk.openFile = "text/csv",
    
    browser.helperApps.neverAsk.saveToDisk = "text/plain,application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    # browser.helperApps.neverAsk.saveToDisk="text/csv",
    
    browser.download.folderList = 2L
                                 )
)


link_to_chart <- "https://www.rba.gov.au/mkt-operations/resources/tech-notes/eligible-securities.html"

remDr <- remoteDriver(remoteServerAddr = "localhost", 
                      port = 4445L, browserName = "firefox", 
                      extraCapabilities = fprof)

remDr$open(silent = TRUE)
remDr$navigate(link_to_chart)
remDr$screenshot(display = TRUE) #This will take a screenshot and display it in the RStudio viewer


#download file - 2 options
remDr$findElement(using ="class", "anchor-xls")$clickElement()
remDr$findElement(using ="xpath", '//*[@id="content"]/section/div[2]/table/tbody/tr[1]/th/a')$clickElement()


remDr$closeWindow()

舊代碼

這是我能想到的最好的代碼:

library(tidyverse)
library(lubridate)

library(XML)
library(readxl)
library(janitor)
library(platus)


library(RSelenium)

firefor_exemptions <- c("application/vnd.openxmlformats-officedocument.presentationml.presentation",
  "application/vnd.openxmlformats-officedocument.presentationml.slide",
  "application/vnd.openxmlformats-officedocument.presentationml.slideshw",
  "application/vnd.openxmlformats-officedocument.presentationml.template",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
  "application/vnd.openxmformats-officedocument.wordprocessingml.document",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
  "application/x-msbinder",
  "application/vnd.ms-officetheme",
  "application/onenote",
  "audio/vnd.ms-playready.media.pya",
  "vdeo/vnd.ms-playready.media.pyv",
  "application/vnd.ms-powerpoint",
  "application/vnd.ms-powerpoint.addin.macroenabled.12",
  "application/vnd.ms-powerpoint.slide.macroenabled.12",
  "application/vnd.ms-powerpoint.presentation.macroenabled.12",
  "appliation/vnd.ms-powerpoint.slideshow.macroenabled.12",
  "application/vnd.ms-project",
  "application/x-mspublisher",
  "application/x-msschedule",
  "application/x-silverlight-app"
)

fprof <- makeFirefoxProfile(list(browser.helperApps.neverAsk.saveToDisk = firefor_exemptions,
                                 browser.download.dir = str_replace_all("c:/users/john_doe/downloads", "/", "\\\\\\\\"))
)



link_to_chart <- "https://www.rba.gov.au/mkt-operations/resources/tech-notes/eligible-securities.html"

remDr <- remoteDriver(remoteServerAddr = "localhost", 
                      port = 4445L, browserName = "firefox", 
                      extraCapabilities = fprof)

remDr$open(silent = FALSE)
remDr$navigate(link_to_chart)

#I'm not sure what I should be selecting
remDr$findElement("class", "js-no-cache anchor-to-file anchor-xls")$clickElement()

remDr$findElement("link", "List of eligible securities")$clickElement()


remDr$closeWindow()

關於如何使這項工作有任何想法嗎?

要在工作目錄中下載chrome ,我們可以使用,

file_path <- getwd() %>% str_replace_all("/", "\\\\\\")

eCaps <- list(
  chromeOptions = 
    list(prefs = list('download.default_directory' = file_path))
)

driver <- rsDriver(browser = "chrome",port = 9995L, extraCapabilities = eCaps)

remDr<-driver[["client"]]
remDr$navigate(link_to_chart)

使用xpath我能夠下載文件,

remDr$findElement(using ="xpath", '//*[@id="content"]/section/div[2]/table/tbody/tr[1]/th/a')$clickElement()

使用class

remDr$findElement(using ="class", 'anchor-xls')$clickElement()

為什么要用Selenium? 如果你找到它的 url,你可以使用download.file下載文件:

url <- "https://www.rba.gov.au/mkt-operations/xls/eligible-securities.xls"
url <- paste0(url, "?v=", gsub(" |:", "-", as.character(Sys.time())))
download.file(url, "securities.xls")

這會將文件保存到磁盤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM