【问题标题】:Scraping coordinate data and additional information for shot locations抓取拍摄位置的坐标数据和附加信息
【发布时间】:2019-11-26 17:44:28
【问题描述】:

我正在通过查看篮球统计数据来了解 R,并且我想提取出投篮图表中显示的信息。

我正在查看以下 D'Angelo Russell 的投篮图表:

https://www.basketball-reference.com/players/r/russeda01/shooting/2019

我正在使用library(rvest) 包中的工具以下列方式抓取数据:

> dlo_html <- read_html("https://www.basketball-reference.com/players/r/russeda01/shooting/2019")
> dlo_nodes1 <- html_nodes(dlo_html, "table")
> dlo_makes <- html_table(dlo_nodes1)

...所以现在当我运行head(dlo_makes) 时,我会从网页左侧的表格中得到一个包含 74 行和 11 列的 data.frame。这是一个不错的开始。

但是,我真正想要的是页面右侧的镜头图表图形中包含的信息。我可以在 html 的源代码中看到它。如果您在源中搜索shot-area,它的正下方大约有 1500 行数据,如下所示:

<div style="top:57px;left:237px;" tip="Oct 17, 2018, BRK at DET<br>1st Qtr, 10:38 remaining<br>Missed 2-pointer from 2 ft<br>BRK leads 2-0" class="tooltip miss">&#215;</div>
<div style="top:154px;left:341px;" tip="Oct 17, 2018, BRK at DET<br>1st Qtr, 10:30 remaining<br>Made 2-pointer from 14 ft<br>BRK now leads 4-0" class="tooltip make">&#9679;</div>
etc.

我是否向html_nodes() 命令传递了不正确的信息?或者我应该使用与html_table 不同的命令来查看节点?还是我在这里缺少其他东西?

【问题讨论】:

  • 可能是因为镜头图中的数据是动态加载的——看这个老post
  • 请注意,本网站的服务条款可能不允许这种使用:sports-reference.com/data_use.html
  • @kstew - 感谢您的链接,这让我有一些思考。
  • @Brian - 感谢您提醒我这一点。但是链接中有一行:“我要指出,学习如何积累数据通常比实际分析数据更有价值,因此我们鼓励您作为学生或专业人士学习如何。”如果可以避免,我不会对他们的网站提出要求,并学会从其他来源获取数据。

标签: r rvest


【解决方案1】:

你想要的数据是作为评论写的,不是动态加载的。

我使用视图源来获取包含数据的 div,它被称为

all_shot-chart

所以这里是得到你想要的代码

dlo_html <- read_html("https://www.basketball-reference.com/players/r/russeda01/shooting/2019")

Commented_Section <- dlo_html%>%html_nodes("[id = 'all_shot-chart']")%>%html_nodes(xpath = 'comment()')%>%
        html_text() %>% read_html() %>%html_node('table')

Missed_Plays <- Commented_Section %>% html_nodes("[class='tooltip miss']")
Maked_Plays <- Commented_Section %>% html_nodes("[class='tooltip make']")

我可以在这个问题中找到如何获得评论部分。

How to read a commented out HTML table using readHTMLTable in R

【讨论】:

  • 这个解决方案让我离我想去的地方更近了。谢谢,@Omar Abd El-Naser。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多