我看到有很多与主题相关的帖子,但我找不到解决方案(肯定是我的错)。
我正在尝试抓取https://tradingeconomics.com上发布的表格,并且作为新手,我正面临问题。
我想根据每页上方的菜单获取所有表格,以及按大陆划分的子表格。
然而,我试图将所有链接包含在一个 R 向量中,然后在那里继续抓取,但没有任何成功:
## 00. Importing the main link
trading_ec <- read_html("https://tradingeconomics.com/indicators")
## 01. Scraping the variables names
tr_ec_tabs <- trading_ec %>%
html_nodes(".list-group-item a") %>%
html_text(trim=TRUE)
## 02. Editing the vector
tr_ec_tabs_lo <- tolower(tr_ec_tabs)
tr_ec_nospace <- gsub(" ", "-", tr_ec_tabs_lo)
## 03. Creating a .json indicators vector
json.indicators <- paste0("https://tradingeconomics.com/country-list/", tr_ec_nospace)
## 04. Function
table <- list()
for(i in seq_along(json.indicators))
{
total_list <- readHTMLTable(json.indicators[i])
n.rows <- unlist(lapply(total_list, function(t) dim(t)[1]))
table[[i]] <- as.data.frame(total[[which.max(n.rows)]])
}