Я новичок в r и rvest. Два дня назад мне помогли с этим кодом, который очищает все имена игроков и работает хорошо. Теперь я пытаюсь добавить код в функцию «fetch_current_players», где она также создает вектор кодов игроков для этого веб-сайта (снятый с URL-адреса). Любая помощь будет принята с благодарностью, так как я провел день в гугле, читал и смотрел видео на YouTube, пытаясь научить себя. Спасибо!
library(rvest)
library(purrr) # flatten/map/safely
library(dplyr) # progress bar
fetch_current_players <- function(letter){
URL <- sprintf("http://www.baseball-reference.com/players/%s/", letter)
pg <- read_html(URL)
if (is.null(pg)) return(NULL)
player_data <- html_nodes(pg, "b a")
player_code<-html_attr(html_nodes(pg, "b a"), "href") #I'm trying to scrape the URL as well as the player name
substring(player_code, 12, 20) #Strips the code out of the URL
html_text(player_data)
player_code #Not sure how to create vector of all codes from all 27 webpages
}
pb <- progress_estimated(length(letters))
player_list <- flatten_chr(map(letters, function(x) {
pb$tick()$print()
fetch_current_players(x)
}))