Webscraping with Rvest, Error in UseMethod("xml_find_first"): no method for 'xml_find_first' applicable for an object of class "character"

Viewed 647

I'm trying to scrape several webpages using the following code

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'))

But I get the following error message

Error in UseMethod("xml_find_first"): no method for 'xml_find_first' applicable for an object of class "character" Traceback:

  1. map(urls_rh, html_node(".div"))
  2. as_mapper(.f, ...)
  3. html_node(".div")
  4. html_node.default(".div")
  5. xml2::xml_find_first(x, make_selector(css, xpath))

I also tried using a for loop, with the same result

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

Any advice?

2 Answers

You can use :

library(rvest)
library(purrr)

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

Or maybe more clearly :

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

We can use lapply from base R

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