将网页中的所有 excel 个文件下载到 R 数据帧
Download all excel files from a webpage to R dataframes
我的问题与this one. I want to download all Excel files (.xlsx) from this webpage非常相似。但不同之处在于(我认为)我没有与示例中使用的相同模式。我尝试了几种变体但没有结果。知道如何下载这些文件吗?此外,如果您能展示我如何将它们直接下载到数据框中(无需先将它们下载到我的 PC),我们将不胜感激。
一种下载 excel 文件的简单方法,一次一个步骤。
首先,获取链接。
library(rvest)
url <- "https://www.fondbolagen.se/fakta_index/statistik/"
read_html(url) |>
html_elements("p") |>
html_elements("a") |>
html_attr("href") |>
(\(x) grep("\.xls", x, value = TRUE))() |>
(\(x) sprintf("http://www.fondbolagen.se%s", x))() -> excel_links
现在,使用this Rich Scriven post中的代码下载文件。我省略了文件创建指令。
dir.create("myexcel")
## save the current directory path for later
wd <- getwd()
## change working directory for the download
setwd("myexcel")
## download them all
lapply(excel_links, \(x) download.file(x, basename(x)))
## reset working directory to original
setwd(wd)
我的问题与this one. I want to download all Excel files (.xlsx) from this webpage非常相似。但不同之处在于(我认为)我没有与示例中使用的相同模式。我尝试了几种变体但没有结果。知道如何下载这些文件吗?此外,如果您能展示我如何将它们直接下载到数据框中(无需先将它们下载到我的 PC),我们将不胜感激。
一种下载 excel 文件的简单方法,一次一个步骤。
首先,获取链接。
library(rvest)
url <- "https://www.fondbolagen.se/fakta_index/statistik/"
read_html(url) |>
html_elements("p") |>
html_elements("a") |>
html_attr("href") |>
(\(x) grep("\.xls", x, value = TRUE))() |>
(\(x) sprintf("http://www.fondbolagen.se%s", x))() -> excel_links
现在,使用this Rich Scriven post中的代码下载文件。我省略了文件创建指令。
dir.create("myexcel")
## save the current directory path for later
wd <- getwd()
## change working directory for the download
setwd("myexcel")
## download them all
lapply(excel_links, \(x) download.file(x, basename(x)))
## reset working directory to original
setwd(wd)