繁体   English   中英

rvest: for loop/map 使用 html_node & html_table 拉多个表

[英]rvest: for loop/map to pull multiple tables using html_node & html_table

我正在尝试以编程方式从NBA 参考(我使用 2020 年 1 月 4 日有多场比赛)中提取给定日期的所有得分。 我首先创建了一个整数列表来表示要提取的框分数的数量:

games<- c(1:3)

然后我使用浏览器中的developer tools来确定每个表包含的内容(您可以使用selector gadget ):

#content > div.game_summaries > div:nth-child(1) > table.team

然后我使用purrr::map创建要拉的表的列表,使用games

map_list<- map(.x= '', paste, '#content > div.game_summaries > div:nth-child(', games, ') > table.teams', 
           sep = "") 
# check map_list
map_list

然后我尝试通过for循环运行这个列表以生成三个表,使用tidyverservest ,它传递了一个错误:

for (i in map_list){
read_html('https://www.basketball-reference.com/boxscores/') %>% 
  html_node(map_list[[1]][i]) %>% 
  html_table() %>% 
  glimpse()
}

Error in selectr::css_to_xpath(css, prefix = ".//") : 
  Zero length character vector found for the following argument: selector
In addition: Warning message:
In selectr::css_to_xpath(css, prefix = ".//") :
  NA values were found in the 'selector' argument, they have been removed

作为参考,如果我明确表示 html 或从map_list调用确切的项目,则代码按预期工作(在项目下方运行以供参考):

read_html('https://www.basketball-reference.com/boxscores/') %>% 
  html_node('#content > div.game_summaries > div:nth-child(1) > table.teams') %>% 
  html_table() %>% 
  glimpse()

read_html('https://www.basketball-reference.com/boxscores/') %>% 
  html_node(map_list[[1]][1]) %>% 
  html_table() %>% 
  glimpse()

我如何使用列表进行这项工作? 我查看了其他线程,但即使它们使用相同的站点,它们也不是相同的问题。

使用您当前的map_list ,如果您想使用for循环,这就是您应该使用的

library(rvest)

for (i in seq_along(map_list[[1]])){
  read_html('https://www.basketball-reference.com/boxscores/') %>% 
   html_node(map_list[[1]][i]) %>% 
   html_table() %>% 
   glimpse()
}

但我认为这更简单,因为paste是矢量化的,因此您不需要使用map来创建map_list

map_list<- paste0('#content > div.game_summaries > div:nth-child(', games, ') > table.teams')
url <- 'https://www.basketball-reference.com/boxscores/'
webpage <- url %>% read_html()

purrr::map(map_list, ~webpage %>% html_node(.x) %>% html_table)

#[[1]]
#       X1  X2    X3
#1 Indiana 111 Final
#2 Atlanta 116      

#[[2]]
#        X1  X2    X3
#1  Toronto 121 Final
#2 Brooklyn 102      

#[[3]]
#       X1  X2    X3
#1  Boston 111 Final
#2 Chicago 104      

这个页面是相当直接的刮。 这是一个可能的解决方案,首先刮取游戏摘要节点“div with class=game_summary”。 这提供了所有玩过的游戏的列表。 这也允许使用保证返回的 html_node 函数,从而保持列表大小相等。

每场比赛总结由三个子表组成,第一、三表可直接抓取。 第二个表没有分配类,因此检索起来更加棘手。

library(rvest)

page <- read_html('https://www.basketball-reference.com/boxscores/')

#find all of the game summaries on the page
games<-page %>% html_nodes("div.game_summary")

#Each game summary has 3 sub tables
#game score is table 1 of class=teams
#the stats is table 3 of class=stats
# the quarterly score is the second table and does not have a class defined
  table1<-games %>%  html_node("table.teams") %>% html_table()
  stats <-games %>%  html_node("table.stats") %>% html_table()
  quarter<-sapply(games, function(g){
                      g %>%  html_nodes("table") %>% .[2] %>% html_table()
                  })
  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM