将for循环的结果返回到一个数据帧

时间:2019-02-11 16:03:28

标签: r for-loop rvest

我正在尝试从profootballreference.com获取所有足球运动员的所有出生地点。我创建了一个forloop,它获取了每个播放器的网址,并且能够将每个播放器的网址放置在单独的csv文件中,然后将它们组合在一起,但是我在如何追加数据帧方面遇到了麻烦,因此无需将它们全部写入csv文件。

library(tidyverse)

library(dplyr)

library(rvest)

### Initial website link where we will get all player names and links
url <- "https://www.pro-football-reference.com/players/"

### letters a to z capitalized, we will then use the paste0() function to get all links for all players names A through Z
letters_CAP <- toupper(letters)
url_2 <- paste0(url, letters_CAP)
url_2
class(url_2)

### Create a for loop to grab every players name and href and add it to a data frame called player_df
for (i in 1:length(url_2)) {
    player_names <- read_html(url_2[i]) %>%  html_nodes("#div_players a") %>% html_text()
    player_links <- read_html(url_2[i]) %>%  html_nodes("#div_players a") %>% html_attr("href")
    player_df <- data.frame(player_names, player_links)
    #un-comment the line below to write to csv
    #write.csv(x = player_df, file = paste0("~/Desktop/player_names/players_", letters_CAP[i],".csv"))
}

我不想将输出写入单个csv文件,而只是创建一个数据帧,其中每一行都包含播放器名称和网址。

0 个答案:

没有答案