我正在尝试从 fbref.com 网站提取一些足球数据,特别是我应该提取一些日期,我想了解如何过滤网站内的各个节点 您好,我想从 fbref 中提取一些数据,但我无法仅提取某种类型的数据。我将通过附加相关的 html 代码来更好地解释:
<th scope="row" class="left " data-stat=**"date"** csk="20230819"><a href="/it/partite/254420f7/Internazionale-Monza-19-Agosto-2023-Serie-A">19-08-2023</a></th>
<a href="/it/partite/254420f7/Internazionale-Monza-19-Agosto-2023-Serie-A">19-08-2023</a>
阅读代码:
url <- https://fbref.com/it/squadre/d609edc0/Statistiche-Internazionale
html_data <- read_html(url)
html_data %>%
html_nodes(".left ")
它或多或少读取 1266 个不同的节点,但我只对提取“data-stats='date'”处的文本感兴趣。通过仅获取这些节点,我应该能够稍后提取“href”之后的日期。
可以使用
html_attr()
函数提取属性中的值并检查是否是所需的值。
library(rvest)
url <- "https://fbref.com/it/squadre/d609edc0/Statistiche-Internazionale"
html_data <- read_html(url)
foundnodes <- html_data %>% html_nodes(".left ")
#extract the attribute and check to see if it is equal to date
nodes_date <- which(html_attr(foundnodes, "data-stat")=="date")
#subset the foundnodes
foundnodes[nodes_date]