R: Webscraping: XML 内容似乎不是 XML: 使用 HTMLParse

R: Webscraping: XML content does not seem to be XML: Using HTMLParse

我一直在尝试网络抓取多年来的数据(以不同的网页为代表)。我的 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)

时出现错误

我不是 100% 确定你想要的输出,你没有在你的例子中包含你的库调用。无论如何,使用此代码您可以获得 table

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")