0

我有一个大列表,其中包含来自以下网络抓取功能的数据。我想将这些列表转换为列名等于节点名的 data.frame。我怎样才能最有效地做到这一点?

生成大列表的代码(可能需要一段时间):

library(rvest)
library(purrr)
library(tidyverse)

### Scraper

scraper <- function(pages){ #pages = 1200 should be enough to cover the year
bribe <- read_html(paste("http://ipaidabribe.com/reports/paid?page", pages, sep = "=")) 
f <- compose(html_text,html_nodes)
all.nodes <- c(".paid-amount span", ".date", ".location", ".transaction a", ".body-copy-lg")
map_df(all.nodes, ~f(bribe, .x))
}
#pages <- seq(10, 1300, by = 10)
#For MWE
pages <- seq(10, 30, by = 10)
bribe.info <- map(pages,~scraper(.x))

访问完整对象:https ://www.filehosting.org/file/details/841630/bribe.RData

4

1 回答 1

0

我想出了一个主意。但它仍然给我一个错误。任何人都可以解决这个问题吗?

r <- bribe.info %>% 
  map_depth(2, na.omit) %>% 
  map(partial(as_tibble, .name_repair = "universal")) %>% 
  bind_rows(.id = "names" )

错误如下:

新名称:* -> ...1 *-> ...2 * -> ...3 *-> ...4 * -> ...5 New names: *-> ...1 * -> ...2 *-> ...3 * -> ...4 *-> ...5 错误:Tibble 列必须具有一致的长度,只有值长度一 > 回收:* 长度 10:列...1, ...2, ...3, ...4 * 长度 11:列...5

我可以看到问题发生的原因,但我不知道如何解决这个问题。在第 5 列中是几个具有不同向量长度的不同向量。

于 2020-01-10T10:02:48.740 回答