在 table 标题不匹配的 table 中用 rvest 抓取 table
scrape a table with rvest in R that has mismatch table heading
我正在尝试抓取这个 table,这看起来非常简单。
这是 table 的 url:https://fantasy.nfl.com/research/scoringleaders?position=1&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek=1
这是我编码的内容:
url <- "https://fantasy.nfl.com/research/scoringleaders?position=1&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek=1"
x = data.frame(read_html(url) %>%
html_nodes("table") %>%
html_table())
这工作正常,但给出了非常奇怪的两行 headers,当我尝试添加 %>% slice(-1) 以取出第一行时,它说我不能,因为它是一个列表。真的很想弄清楚如何做到这一点。
这是一种解决方案。解释如下。
library(rvest)
library(tidyverse)
read_html(url) %>%
html_nodes("table") %>%
html_table(header = T) %>%
simplify() %>%
first() %>%
setNames(paste0(colnames(.), as.character(.[1,]))) %>%
slice(-1)
glimpse()
的输出:
Observations: 25
Variables: 16
$ Rank <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"…
$ Player <chr> "Lamar Jackson QB - BAL", "Dak Prescott QB - DAL", "Deshaun W…
$ Opp <chr> "@MIA", "NYG", "@NO", "@ARI", "@JAX", "@PHI", "PIT", "WAS", "…
$ PassingYds <chr> "324", "405", "268", "385", "378", "380", "341", "313", "248"…
$ PassingTD <chr> "5", "4", "3", "3", "3", "3", "3", "3", "3", "3", "2", "2", "…
$ PassingInt <chr> "-", "-", "1", "-", "-", "-", "-", "-", "-", "1", "1", "1", "…
$ RushingYds <chr> "6", "12", "40", "22", "2", "-", "-", "5", "24", "6", "13", "…
$ RushingTD <chr> "-", "-", "1", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ ReceivingRec <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ ReceivingYds <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ ReceivingTD <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ RetTD <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ MiscFumTD <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ Misc2PT <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "1", "-", "…
$ FumLost <chr> "-", "-", "-", "1", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ FantasyPoints <chr> "33.56", "33.40", "30.72", "27.60", "27.32", "27.20", "25.64"…
说明
来自 ?html_table
文档:
html_table
currently makes a few assumptions:
- No cells span multiple rows
- Headers are in the first row
通过在 html_table()
中设置 header = TRUE
可以解决您的部分问题。
问题的另一部分是 header 单元格跨越两行,这是 html_table()
不期望的。
假设您不想丢失任何 header 行中的信息,您可以:
- 使用
simplify
和first
从html_table
获得的列表中提取数据框
- 使用
setNames
合并两个header行(现在是数据框列和第一行)
- 用
slice
删除第一行(现在是多余的)
我正在尝试抓取这个 table,这看起来非常简单。 这是 table 的 url:https://fantasy.nfl.com/research/scoringleaders?position=1&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek=1
这是我编码的内容:
url <- "https://fantasy.nfl.com/research/scoringleaders?position=1&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek=1"
x = data.frame(read_html(url) %>%
html_nodes("table") %>%
html_table())
这工作正常,但给出了非常奇怪的两行 headers,当我尝试添加 %>% slice(-1) 以取出第一行时,它说我不能,因为它是一个列表。真的很想弄清楚如何做到这一点。
这是一种解决方案。解释如下。
library(rvest)
library(tidyverse)
read_html(url) %>%
html_nodes("table") %>%
html_table(header = T) %>%
simplify() %>%
first() %>%
setNames(paste0(colnames(.), as.character(.[1,]))) %>%
slice(-1)
glimpse()
的输出:
Observations: 25
Variables: 16
$ Rank <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"…
$ Player <chr> "Lamar Jackson QB - BAL", "Dak Prescott QB - DAL", "Deshaun W…
$ Opp <chr> "@MIA", "NYG", "@NO", "@ARI", "@JAX", "@PHI", "PIT", "WAS", "…
$ PassingYds <chr> "324", "405", "268", "385", "378", "380", "341", "313", "248"…
$ PassingTD <chr> "5", "4", "3", "3", "3", "3", "3", "3", "3", "3", "2", "2", "…
$ PassingInt <chr> "-", "-", "1", "-", "-", "-", "-", "-", "-", "1", "1", "1", "…
$ RushingYds <chr> "6", "12", "40", "22", "2", "-", "-", "5", "24", "6", "13", "…
$ RushingTD <chr> "-", "-", "1", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ ReceivingRec <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ ReceivingYds <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ ReceivingTD <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ RetTD <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ MiscFumTD <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ Misc2PT <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "1", "-", "…
$ FumLost <chr> "-", "-", "-", "1", "-", "-", "-", "-", "-", "-", "-", "-", "…
$ FantasyPoints <chr> "33.56", "33.40", "30.72", "27.60", "27.32", "27.20", "25.64"…
说明
来自 ?html_table
文档:
html_table
currently makes a few assumptions:
- No cells span multiple rows
- Headers are in the first row
通过在 html_table()
中设置 header = TRUE
可以解决您的部分问题。
问题的另一部分是 header 单元格跨越两行,这是 html_table()
不期望的。
假设您不想丢失任何 header 行中的信息,您可以:
- 使用
simplify
和first
从html_table
获得的列表中提取数据框
- 使用
setNames
合并两个header行(现在是数据框列和第一行) - 用
slice
删除第一行(现在是多余的)