首页 > 解决方案 > 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”方法。

标签: rweb-scrapingrvest

解决方案


正如 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 书面同意,改编、转移、分发、下载或存储本网站的任何内容(包括如下所述的比赛信息),或将本网站的任何部分并入另一个网站。


推荐阅读