【发布时间】:2018-07-14 00:44:47
【问题描述】:
我有以下链接,成功抓取,我想创建一个for 循环。我正在抓取的表格分布在 3 页上,因此 whip_2018a、whip_2018b 和 whip_2018c 链接向量:
library(tidyverse)
library(rvest)
whip_2018a <- "http://www.espn.com/mlb/stats/pitching/_/order/false"
whip_2018b <- "http://www.espn.com/mlb/stats/pitching/_/count/41/qualified/true/order/false"
whip_2018c <- "http://www.espn.com/mlb/stats/pitching/_/count/81/qualified/true/order/false"
这些链接是 2018 年的季节,但我的链接可以追溯到 2005 年。如何将以下代码转换为 for 循环,以考虑多个页面链接和多年/季节?强>
# Scraping 2018 tables - there are multiple pages to the list
a2018 <- whip_2018a %>%
read_html %>%
html_node("#my-players-table > div > div.mod-content > table") %>%
html_table(header = T)
b2018 <- whip_2018b %>%
read_html %>%
html_node("#my-players-table > div > div.mod-content > table") %>%
html_table(header = T)
c2018 <- whip_2018c %>%
read_html %>%
html_node("#my-players-table > div > div.mod-content > table") %>%
html_table(header = T)
# This creates the table for the 2018 season
whip_2018 <- rbind(a2018, b2018, c2018)
如果这不可能,我理解,但我的直觉告诉我,一些位置合适的 [i] 可以使这项工作发挥作用。供参考,2017的链接在这里(大多数季节至少有两个或三个链接):
whip_2017a <- "http://www.espn.com/mlb/stats/pitching/_/year/2017/order/false"
whip_2017b <- "http://www.espn.com/mlb/stats/pitching/_/year/2017/count/41/qualified/true/order/false")
WHIP 是 MLB 统计数据,因此是我的向量名称。
【问题讨论】:
标签: r loops dplyr lapply rvest