如何使用 R 中的 purrr 映射函数将 xml-nodesets(使用 rvest 创建)放入 tibble?

How do I put xml-nodesets (created with rvest) into a tibble using purrr's map-function in R?

我想抓取大量网站。为此,我首先阅读网站的 html-脚本并将它们存储为 xml_nodesets。由于我只需要网站的内容,所以我最后从 xml_nodesets 中提取了每个网站的内容。为此,我编写了以下代码:

# required packages
library(purrr)
library(dplyr)
library(xml2)
library(rvest)
    
# urls of the example sources
test_files <- c("https://en.wikipedia.org/wiki/Web_scraping", "https://en.wikipedia.org/wiki/Data_scraping")
        
# reading in the html sources, storing them as xml_nodesets
test <- test_files %>% 
map(., ~ xml2::read_html(.x, encoding = "UTF-8"))
        
# extracting selected nodes (contents)
test_tbl <- test %>%
     map(., ~tibble(
     # scrape contents
     test_html = rvest::html_nodes(.x, xpath = '//*[(@id = "toc")]')  
            ))

不幸的是,这会产生以下错误:

Error: All columns in a tibble must be vectors.
x Column `test_html` is a `xml_nodeset` object.

我想我理解这个错误的实质,但我找不到解决方法。这也有点奇怪,因为我在 1 月份能够顺利 运行 这段代码,突然它就不能用了。我怀疑包更新是原因,但安装旧版本的 xml2rvesttibble 没有也不帮忙。此外,仅抓取一个网站也不会产生任何错误:

test <- read_html("https://en.wikipedia.org/wiki/Web_scraping", encoding = "UTF-8") %>%
  rvest::html_nodes(xpath = '//*[(@id = "toc")]')

你对如何解决这个问题有什么建议吗?非常感谢!

编辑: 我从 ...

中删除了 %>% html_text
test_tbl <- test %>%
     map(., ~tibble(
     # scrape contents
     test_html = rvest::html_nodes(.x, xpath = '//*[(@id = "toc")]')  
            ))

... 因为这不会产生此错误。不过,编辑后的代码确实如此。

您需要将对象存储在列表中。

test %>%
  purrr::map(~tibble(
    # scrape contents
    test_html = list(rvest::html_nodes(.x, xpath = '//*[(@id = "toc")]'))  
  ))

#[[1]]
# A tibble: 1 x 1
#  test_html 
#  <list>    
#1 <xml_ndst>

#[[2]]
# A tibble: 1 x 1
#  test_html 
#  <list>    
#1 <xml_ndst>