使用 Rvest 从滚动 table 中提取数据

Extracting data from scrolling table using Rvest

我希望从位于 https://thearcfooty.com/2017/01/28/a-complete-history-of-the-afl/

的 table 中提取所有记录

我面临的挑战是它是一个滚动 table(文本显示在 table 的底部,它包含 31,228 条记录:

Showing 1 to 10 of 31,228 entries

我是 Rvest 的新手,在检查 Google Chrome 中的 table 后尝试了以下操作:

library(rvest)
url <- "https://thearcfooty.com/2017/01/28/a-complete-history-of-the-afl/"

Table  <- url %>%
  read_html() %>%
  html_nodes(xpath= '//*[@id="table_1"]') %>%
  html_table()
TableNew <- Table[[1]]
TableNew 

但它一直挂着。理想情况下,我想 return 一个包含所有行和所有列的所有记录的数据框。

我的猜测是 html_table 中的一些代码有点慢,所以它会无休止地运行。实际上,您可以读入所有文本并转换为数据框形状。我还没有检查结果是否正确。但是根据我观察的一些例子,应该没问题。

library(rvest)
#> Loading required package: xml2
library(data.table)
url <- "https://thearcfooty.com/2017/01/28/a-complete-history-of-the-afl/"

page <- read_html(url)

tb_str <- page %>% 
  html_nodes(css = 'tr') %>% 
  html_text()

dt <- data.table(raw=tb_str)

headers <- strsplit(tb_str[1],split = "\W+")[[1]]
dt[,(headers):=tstrsplit(raw,split="\n +")]
dt[,raw:=NULL]
str(dt[!is.na(season)])
#> Classes 'data.table' and 'data.frame':   31228 obs. of  14 variables:
#>  $ date             : chr  "08/05/1897" "08/05/1897" "08/05/1897" "08/05/1897" ...
#>  $ season           : chr  "1897" "1897" "1897" "1897" ...
#>  $ round            : chr  "1" "1" "1" "1" ...
#>  $ home_away        : chr  "A" "A" "A" "A" ...
#>  $ team             : chr  "CA" "SK" "ME" "ES" ...
#>  $ opponent         : chr  "FI" "CW" "SY" "GE" ...
#>  $ margin_pred      : chr  "0.00" "0.00" "0.00" "-2.99" ...
#>  $ margin_actual    : chr  "-33.00" "-25.00" "17.00" "23.00" ...
#>  $ win_prob         : chr  "0.50" "0.50" "0.50" "0.47" ...
#>  $ result           : chr  "0.18" "0.24" "0.69" "0.74" ...
#>  $ team_elo_pre     : chr  "1500" "1500" "1500" "1500" ...
#>  $ opponent_elo_pre : chr  "1500" "1500" "1500" "1500" ...
#>  $ team_elo_post    : chr  "1473" "1478" "1515" "1522" ...
#>  $ opponent_elo_post: chr  "1526" "1521" "1484" "1477" ...
#>  - attr(*, ".internal.selfref")=<externalptr>

reprex package (v0.3.0)

于 2020-07-27 创建