r语言 - 使用 Rvest 进行网络抓取,使用方法( "xml_find_first" 中的错误):没有适用于类 "character" 对象的'xml_find_first'方法



我正在尝试使用以下代码抓取几个网页

library(rvest)
library(dplyr)

glasgow <- 'https://glasgow.gov.uk/article/24653/European-Settlement-Service'
rsg <- 'https://www.romasupportgroup.org.uk/roma-and-brexit.html'

urls<-c(rsg, glasgow)
urls_rh<-map(urls, read_html)

text <- map(urls_rh, html_node('.div'))

但我收到以下错误消息

UseMethod("xml_find_first"(中的错误:没有适用于类"的对象的"xml_find_first"的方法;字符";追溯:

  1. 映射(urls_rh,html_node(".div"((
  2. as_mapper(.f,…(
  3. html节点(".div"(
  4. html_node.default(".div"(
  5. xml2::xml_find_first(x,make_selector(css,xpath((

我还尝试使用for循环,结果与相同

for (i in urls_rh){ 
text <- html_node("div")
}

有什么建议吗?

您可以使用:

library(rvest)
library(purrr)
text <- map(urls_rh, html_node, 'div')

或者更清楚地说:

text <- map(urls_rh, ~html_node(., 'div'))

我们可以从base R使用lapply

text <- lapply(urls_rh, function(x) html_node(x, "div"))

最新更新