我正在尝试从相同的网站导入数据库,但在不同的选项卡中。
# webscraping para idh
algo <- c(1996:2017)
idh_link <- c(paste0("https://datosmacro.expansion.com/idh?anio=", 1996:2017))
final <- vector(length = length(idh_link))
for (i in seq_along(algo)) {
idh_desc <- read_html(idh_link[i])
pais <- idh_desc %>%
html_nodes("td:nth-child(1), .header:nth-child(1)") %>%
html_text()
idhaño <- idh_desc %>%
html_nodes("td:nth-child(2), .header:nth-child(2)") %>%
html_text()
final[i] <- tibble(pais, idhaño)
}
在这种情况下,它只从第一个链接恢复信息,而不是在循环结束时创建tibble (其思想是对所有的tibble进行内部连接)。
我正在使用library(rvest)
进行网络抓取
发布于 2021-05-16 19:00:41
向量不能存储数据。帧/tibble。向量只能存储原子对象,如整数、字符串等。
要存储一系列数据帧,最好使用列表。
algo <- c(1996:2017)
idh_link <- c(paste0("https://datosmacro.expansion.com/idh?anio=", 1996:2017))
#data structure to store a series of data frames
final <- list()
for (i in seq_along(algo)) {
idh_desc <- read_html(idh_link[i])
pais <- idh_desc %>%
html_nodes("td:nth-child(1), .header:nth-child(1)") %>%
html_text()
idhaño <- idh_desc %>%
html_nodes("td:nth-child(2), .header:nth-child(2)") %>%
html_text()
#name the list elements with the year information
final[[as.character(algo[i])]] <- tibble(pais, idhaño)
#add a pause so not to "attack" the server
Sys.sleep(1)
}
要组合列表中存储的所有数据帧,我建议使用dplyr包中的bind_rows()
或bind_cols()
。
https://stackoverflow.com/questions/67559520
复制相似问题