Skip to content
ATP Player Rankings
install.packages("RSelenium")
install.packages("tidyverse")
install.packages("openxlsx")
# Load necessary libraries
library(RSelenium)
library(rvest)
library(openxlsx)
# Start a Selenium server and open a Chrome browser
driver <- rsDriver(browser = c("chrome"), chromever = "latest")
remDr <- driver[["client"]]
# Navigate to the ATP rankings webpage
url <- "http://www.espn.com/sports/tennis/rankings"
remDr$navigate(url)
# Extract the table data using rvest
page <- read_html(remDr$getPageSource()[[1]])
table_data <- page %>% html_node("#my-players-table") %>% html_table()
# Extract the rank and player columns
rankings <- table_data[, c("RK", "PLAYER")]
# Rename the columns
colnames(rankings) <- c("rk", "player")
# Close the browser and stop the Selenium server
remDr$close()
driver$stopServer()
# Create a data frame called "2023_atp_rankings" with the scraped data
2023_atp_rankings <- rankings
# Export the data frame to Excel
write.xlsx(2023_atp_rankings, file = "ATP Player Rankings.xlsx")