RSelenium从站点提取文本

时间:2019-09-25 20:45:07

标签: r rselenium

我需要从网站复制并粘贴文本。我目前正在使用下面的代码打开浏览器会话,然后从页面中选择文本。但是,我在选择文本后仍无法弄清楚如何复制文本。

library(RSelenium)

rD <- rsDriver(browser = "chrome"
           ,verbose = TRUE
           ,chromever = "73.0.3683.68")

 Sys.sleep(3)

remDr <- rD[["client"]]

remDr$navigate("https://cran.r-project.org/web/packages/RSelenium/vignettes/basics.html")

remDr$sendKeysToActiveElement(list(key = 'control',"a", key = 'control')) # select text

我需要了解如何从剪贴板复制和读取文本。

1 个答案:

答案 0 :(得分:0)

以下代码从页面复制文本

library(RSelenium)

rD <- rsDriver(browser = "chrome"
       ,verbose = TRUE
       ,chromever = "73.0.3683.68")

 Sys.sleep(3)

remDr <- rD[["client"]]

remDr$navigate("https://cran.r-project.org/web/packages/RSelenium/vignettes/basics.html")

remDr$sendKeysToActiveElement(list(key = 'control',"a", key = 'control')) # select text

remDr$sendKeysToActiveElement(list(key = 'control',"c", key = 'control')) # copy text to memory

df <- read.table(file = "clipboard", # save to df from clipboard
         header = F,
        skip = 2,
        fill = TRUE,
         sep = "\t")
dim(df)
[1] 260   1