r - 使用 Rvest 进行网页抓取,UseMethod(“xml_find_first”)中的错误:没有适用于“字符”类对象的“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”方法 Traceback:
- 地图(urls_rh,html_node(“.div”))
- as_mapper(.f, ...)
- html_node(".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'))
推荐阅读
- sql-server - Azure MS SQL 通过 Workbench 导入 MySQL - 没有架构?
- orientdb - OrientDB:如何实现分页?
- javascript - 如何在单个解决方案中运行多个 Karma 和 Jasmine 项目(需要 Jenkins 集成)?
- r - 当逻辑回归不能完全捕获数据时会发生什么?
- google-calendar-api - Google Calendar API 使用受限问题
- swift - 从 CATransform3D 获取缩放、平移和旋转
- lua - 在嵌套情况下如何最好地找到特定值的键?
- php - Laravel 购票支付方式
- html - 无法在 @font-face 中加载 base64 编码字体
- python - readthedocs autobuild 需要 Fortran 编译器:改为上传本地构建的文件?