【问题标题】:Scraping sports data frames抓取运动数据帧
【发布时间】:2021-05-12 01:34:45
【问题描述】:

所以我的问题是我想分别刮投手台和击球台。当我使用选择器小工具时,投手和击球台都使用“.has-right-shadow”。我试图用我拥有的代码拉表,但它说没有可用的数据。

library(rvest)

pitcher_url <- 'https://www.rotowire.com/baseball/stats.php'
pitcher_stats <- read_html(pitcher_url)

pitcher_df <- html_nodes(pitcher_stats,".has-right-shadow")
pitcher_table <- html_text(pitcher_df)

View(pitcher_table) 

【问题讨论】:

  • 如果可能的话,更简单的方法就是下载表格底部提供的 csv 文件。

标签: r web-scraping rvest


【解决方案1】:

数据很容易通过查询字符串作为 json 检索。只需更改查询字符串中的 pos(位置)参数即可。

library(jsonlite)
library(purrr)
library(magrittr)

batters <- jsonlite::read_json('https://www.rotowire.com/baseball/tables/player-basic-stats.php?pos=B&league=3&season=2021&filter=0')
pitchers <- jsonlite::read_json('https://www.rotowire.com/baseball/tables/player-basic-stats.php?pos=P&league=3&season=2021&filter=0')

df_batters <- map_dfr(batters, data.frame)
df_pitchers <- map_dfr(pitchers, data.frame)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多