我正在尝试从该网站上抓取表格,rvest返回空列表。它适用于其他网站。可能是什么问题?
谢谢
library(rvest)
urlONGov <- "https://www.ontario.ca/page/2019-novel-coronavirus"
ONGov <- urlONGov %>%
xml2::read_html() %>%
html_nodes(xpath='//*[@id="pagebody"]/table[1]') %>%
html_table()
ONGov
答案 0 :(得分:2)
该表是从刷新网页时可在开发工具网络标签中找到的API调用中动态检索的。调用返回json,您需要遍历json来检索一些HTML,然后可以将其解析出表格。
library(jsonlite)
library(rvest)
table <- jsonlite::read_json('https://api.ontario.ca/api/drupal/page%2F2019-novel-coronavirus?fields=nid,field_body_beta,body')%>%
.$body%>%.$und%>%.[[1]]%>%.$safe_value%>%
read_html()%>%html_node('table')%>%html_table()