我试图通过网络抓取多年来的数据(由不同的网页表示)。我的2019年数据完全按照我想要的那样工作,但当我尝试像2019年数据一样准备2016年的数据时,我得到了一个错误。
url19 <- 'https://www.pro-football-reference.com/draft/2019-combine.htm'
get_pfr_HTML_file19 <- GET(url19)
combine.parsed19 <- htmlParse(get_pfr_HTML_file19)
page.tables19 <- readHTMLTable(combine.parsed19, stringsAsFactors = FALSE)
data19 <- data.frame(page.tables19[1])
cleanData19 <- data19[!rowSums(data19 == "")> 0,]
cleanData19 <- filter(cleanData19, cleanData19$combine.Pos == 'CB' | cleanData19$combine.Pos == 'S')cleanData19正是我想要的,但是当我尝试用2016年的数据运行它时,我得到了错误: XML内容似乎不是XML:'‘
url16 <- 'https://www.pro-football-reference.com/draft/2016-combine.htm'
get_pfr_HTML_file16 <- GET(url16)
combine.parsed16 <- htmlParse(get_pfr_HTML_file16)
page.tables16 <- readHTMLTable(combine.parsed16, stringsAsFactors = FALSE)
data16 <- data.frame(page.tables16[1])
cleanData16 <- data16[!rowSums(data16 == "")> 0,]
cleanData16 <- filter(cleanData16, cleanData16$combine.Pos == 'CB' | cleanData16$combine.Pos == 'S')尝试运行combine.parsed16 <- htmlParse(get_pfr_HTML_file16)时出现错误
发布于 2020-11-14 09:32:49
我不能100%确定你想要的输出,你没有在你的例子中包括你的库调用。无论如何,使用下面的代码可以获得该表
library(rvest)
library(dplyr)
url <- 'https://www.pro-football-reference.com/draft/2016-combine.htm'
read_html(url) %>%
html_nodes(".stats_table") %>%
html_table() %>%
as.data.frame() %>%
filter(Pos == 'CB' | Pos == "S")几年一次:
library(rvest)
library(magrittr)
library(dplyr)
library(purrr)
years <- 2013:2019
urls <- paste0(
'https://www.pro-football-reference.com/draft/',
years,
'-combine.htm')
map(
urls,
~read_html(.x) %>%
html_nodes(".stats_table") %>%
html_table() %>%
as.data.frame()
) %>%
set_names(years) %>%
bind_rows(.id = "year") %>%
filter(Pos == 'CB' | Pos == "S")https://stackoverflow.com/questions/64829458
复制相似问题