我正在尝试使用以下代码抓取几个网页
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"的方法;字符";追溯:
- 映射(urls_rh,html_node(".div"((
- as_mapper(.f,…(
- html节点(".div"(
- html_node.default(".div"(
- 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"))