r - rvest r 数据抓取返回空表
问题描述
编程新手并尝试从以下站点删除数据。当我运行下面的代码时,它返回一个空的数据集或表。任何帮助或替代方案将不胜感激。
url <- "https://fasttrack.grv.org.au/Dog/Form?id=2003010003"
tab <- url %>% read_html %>%
html_node("dogruns_wrapper") %>%
html_text()
View(tab)
已尝试使用 xpath 和相同的结果,并且 html_table() 而不是文本返回错误,即没有适用于“xml_missing”类对象的“html_table”方法。
解决方案
正如 Mislav 所说,该表是使用 JavaScript 生成的,因此您最好的选择是RSelenium
.
另外,如果要获取表格,使用html_table()
.
我的尝试:
# Load packages
library(rvest) #Loading the rvest package
library(magrittr) # for the '%>%' pipe symbols
library(RSelenium) # to get the loaded html of the webpage
# starting local RSelenium (this is the only way to start RSelenium that is working for me atm)
selCommand <- wdman::selenium(jvmargs = c("-Dwebdriver.chrome.verboseLogging=true"), retcommand = TRUE)
shell(selCommand, wait = FALSE, minimized = TRUE)
remDr <- remoteDriver(port = 4567L, browserName = "chrome")
remDr$open()
# define url
url <- "https://fasttrack.grv.org.au/Dog/Form?id=2003010003"
# go to website
remDr$navigate(url)
# as it's being loaded with JavaScript and it has a slow load, add a sleep here
Sys.sleep(10) # increase as needed
# get the html object of the webpage
html_obj <- remDr$getPageSource(header = TRUE)[[1]] %>% read_html()
# read the table in the html_obj
tab <- html_obj %>% html_table() %>% .[[1]]
希望能帮助到你!但是,请务必在执行之前检查网页是否允许抓取!检查条款和条件:
除了出于个人使用或本网站或这些条款和条件中另有说明的直接目的查看、打印、访问或与本网站互动外,您不得复制、复制、修改、与公众交流、未经 GRV 书面同意,改编、转移、分发、下载或存储本网站的任何内容(包括如下所述的比赛信息),或将本网站的任何部分并入另一个网站。
推荐阅读
- mysql - 似乎无法弄清楚这个mysql错误。在正文中声明。任何帮助都是极好的
- python - 为什么我执行 PCA 后图像文件的大小会增加?
- javascript - 如何为包含制作不敏感的值类型?
- python - 每次运行程序时都有不同的cloudpickle
- discord - 我有这个代码,但我想制作它,这样它就可以给出机器人加入的服务器上有多少成员,它不会给我一个错误
- lua - 如何修复“ServerScriptService.Earthquake:11:尝试使用 'OnServerEvent' 索引 nil”
- python - 如何检查数组的所有值是否彼此相等?
- r - tableone 包:如何从 svydesign 对象中获取优势比和置信区间?
- javascript - 在 API 调用和更改状态后,React 不会重新渲染
- java - 以二进制数计算 0 的序列