我正在尝试在以下网站上抓取数据框

http://stats.nba.com/game/0041700404/playbyplay/

我想创建一个表格,其中包含比赛的日期,整个比赛的得分以及球队名称

我正在使用以下代码:

game1 <- read_html("http://stats.nba.com/game/0041700404/playbyplay/")

#Extracts the Date
html_nodes(game1, xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "game-summary-team--vtm", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "game-summary-team__lineup", " " ))]')

#Extracts the Score
html_nodes(game1, xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "status", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "score", " " ))]')

#Extracts the Team names
html_nodes(game1, xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "game-summary-team__name", " " ))]//a')


不幸的是,我得到以下

{xml_nodeset (0)}
{xml_nodeset (0)}
{xml_nodeset (0)}


我已经看到了很多关于这个问题的问题和答案,但是似乎都没有帮助。

最佳答案

不幸的是,rvest在动态创建的JavaScript页面中无法很好地发挥作用。它最适合静态HTML网页。

我建议看一下RSelenium。最后,我使用rsDriver从页面中获取了一些内容

代码样例:

library(RSelenium)
rD <- rsDriver() # runs a chrome browser, wait for necessary files to download
remDr <- rD$client
#no need for remDr$open() browser should already be open
remDr$navigate("http://stats.nba.com/game/0041700404/playbyplay/")

teams <- remDr$findElement(using = "xpath", "//span[@class='team-full']")
teams$getElementText()[[1]]
# and so on...

remDr$close()
# stop the selenium server
rD[["server"]]$stop()
# if user forgets to stop server it will be garbage collected.
rD <- rsDriver()
rm(rD)
gc(rD)


等等...

PS:在使用当前R的Windows上安装时遇到了一些麻烦
*此worked
* How to set up rselenium for R?

08-24 18:06