如何从选项卡式 ESPN table 中提取球员统计数据?

How can I pull player stats from a tabbed ESPN table?

我一直在阅读其他一些关于使用 R 从 ESPN 提取球员和比赛数据的有用指南,但是我遇到了选项卡式 tables 的问题。如最近一场橄榄球比赛的球员统计数据 here 所示,球员统计数据 table 分为 'Scoring'、'Attacking'、'Defending' 和 'Discipline'.

使用以下代码(在两个可爱的包(RCurl 和 htmltab)的帮助下,我可以从该页面拉出第一个选项卡 ('Scoring') ...

# install & attach RCurl
if (!base::require(package="RCurl")) utils::install.packages("RCurl")
library(RCurl)
# install & attach htmltab
if (!base::require(package="htmltab")) utils::install.packages("htmltab")
library(htmltab)

# assign URL
theurl <- RCurl::getURL("https://www.espn.co.uk/rugby/playerstats?gameId=294854&league=270557",.opts = list(ssl.verifypeer = FALSE))
# pull tables from url
team1 <- htmltab::htmltab(theurl,which=1)
team2 <- htmltab::htmltab(theurl,which=2)
league <- htmltab::htmltab(theurl,which=3)

...格式如下,正是我想要的...

    
team1

rowID LEINS Tx TA CG PG PTS
2   J LarmourFB 0   0   0   0   0   0
3   H KeenanW   0   0   0   0   0   0
4   G RingroseC 0   0   0   0   0   0
5   R HenshawC  1   0   0   0   0   5
6   J LoweW 1   0   0   0   0   5
7   R ByrneFH   0   0   2   2   0   10
8   J Gibson-ParkSH 0   1   0   0   0   0
9   C HealyP    0   0   0   0   0   0
10  R KelleherH 0   0   0   0   0   0
11  A PorterP   0   0   0   0   0   0

...但是我似乎无法拉出除 'Scoring' 以外的任何选项卡。我确定我遗漏了一些非常明显的东西,所以非常感谢有人指出我哪里出错了!

提前致谢!

如果您查看来源 html 页面,您会发现数据不在开头。您可以找到一个 data-reactid 标签,表示只有在您单击新选项卡后才会加载数据。所以你需要找到一种方法来点击第二个选项卡。

您的一个选择可能是使用 Selenium:https://www.rdocumentation.org/packages/RSelenium/versions/1.7.7 这将使您能够进行必要的按钮点击。

可在此处找到示例:https://www.r-bloggers.com/2014/12/scraping-with-selenium/