我有一个情况,想要在不同的网址上抓取多个表格。我已经成功地抓取了一个页面,但是当我尝试跨页面抓取并将表格堆叠为数据框/列表时,我的函数失败了。
library(rvest)
library(tidyverse)
library(purrr)
index <-225:227
urls <- paste0("https://lsgkerala.gov.in/en/lbelection/electdmemberdet/2010/", index)
get_gram <- function(url){
urls %>%
read_html() %>%
html_nodes(xpath = '//*[@id="block-zircon-content"]/a[2]') %>%
html_text() -> temp
urls %>%
read_html() %>%
html_nodes(xpath = '//*[@id="block-zircon-content"]/table') %>%
html_table() %>%
as.data.frame() %>% add_column(newcol=str_c(temp))
}
#results <- map_df(urls,get_gram) Have commented this out, but this is what i
# used to get the table when the index just had one element and it worked.
results <- list()
results[[i]] <- map_df(urls,get_gram)
我觉得我在堆叠map_df输出的步骤上有些踌躇不前,提前感谢你的帮助!
page <- url %>% read_html()
пјҢ然еҗҺи§Јжһҗ "page" иҺ·еҸ–жүҖйңҖзҡ„дҝЎжҒҜгҖӮ - Dave2e