Web scraping 如何通过使用rvest动态更新url从多个页面中获取数据

Web scraping 如何通过使用rvest动态更新url从多个页面中获取数据,web-scraping,rvest,Web Scraping,Rvest,我正试图从中提取数据。我感兴趣的是按年份从草稿选择中提取数据。年份从1963年到2018年。 url结构中有一种常见的模式。例如,它的https://www.eliteprospects.com/draft/nhl-entry-draft/2018,https://www.eliteprospects.com/draft/nhl-entry-draft/2017等等 到目前为止,我已经成功地提取了一年的数据。我已经编写了一个自定义函数,在给定输入的情况下,scraper将收集数据并以好看的数据帧

我正试图从中提取数据。我感兴趣的是按年份从
草稿选择中提取数据。年份从1963年到2018年。
url结构中有一种常见的模式。例如,它的
https://www.eliteprospects.com/draft/nhl-entry-draft/2018
https://www.eliteprospects.com/draft/nhl-entry-draft/2017
等等

到目前为止,我已经成功地提取了一年的数据。我已经编写了一个自定义函数,在给定输入的情况下,scraper将收集数据并以好看的数据帧格式呈现

library(rvest)
library (tidyverse)
library (stringr)
get_draft_data<- function(draft_type, draft_year){

  # replace the space between words in draft type with a '-'
  draft_types<- draft_type %>%
    # coerce to tibble format
    as.tibble() %>%
    set_names("draft_type") %>% 
    # replace the space between words in draft type with a '-'
    mutate(draft_type = str_replace_all(draft_type, " ", "-"))

  # create page url
  page <- stringr::str_c("https://www.eliteprospects.com/draft/", draft_types, "/", draft_year)%>%
    read_html()

  # Now scrape the team data from the page
  # Extract the team data
  draft_team<- page %>%

    html_nodes(".team") %>%
    html_text()%>%
    str_squish() %>%
    as_tibble()

  # Extract the player data
  draft_player<- page %>%

    html_nodes("#drafted-players .player") %>%
    html_text()%>%
    str_squish() %>%
    as_tibble()

  # Extract the seasons data
  draft_season<- page %>%

    html_nodes(".seasons") %>%
    html_text()%>%
    str_squish() %>%
    as_tibble()

# Join the dataframe's together. 
  all_data<- cbind(draft_team, draft_player,draft_season)  

  return(all_data)

} # end function

# Testing the function
draft_data<-get_draft_data("nhl entry draft", 2011)
glimpse(draft_data)
Observations: 212
Variables: 3
$ value <chr> "Team", "Edmonton Oilers", "Colorado Avalanche", "Florida Panth...
$ value <chr> "Player", "Ryan Nugent-Hopkins (F)", "Gabriel Landeskog (F)", "...
$ value <chr> "Seasons", "8", "8", "7", "8", "6", "8", "8", "8", "7", "7", "3...
库(rvest)
图书馆(tidyverse)
图书馆(stringr)
获取\u草稿\u数据%
设置名称(“草稿类型”)%>%
#将草稿类型中单词之间的空格替换为“-”
变异(草稿类型=str替换所有草稿类型,“,”-”)
#创建页面url
页数%
read_html()
#现在从页面中刮取团队数据
#提取团队数据
草稿组%
html_节点(“.team”)%>%
html_text()%>%
str_squish()%>%
作为_tible()
#提取播放器数据
选秀球员%
html#U节点(“#drafted players.player”)%>%
html_text()%>%
str_squish()%>%
作为_tible()
#提取季节数据
选秀季%
html_节点(“.seasures”)%>%
html_text()%>%
str_squish()%>%
作为_tible()
#将数据帧连接在一起。

所有的_数据我只需要创建一个函数,对给定的年份进行刮取,然后绑定该年份的行

  • 使用
    paste()
    创建一个包含该年字符串和变量的动态url
  • 为url编写scrape函数(注意:您不必使用html_文本——它存储为一个表,因此可以使用
    html_table()
    直接提取它)
  • 使用
    lappy()
  • 使用
    bind\u rows()
  • 以下是2010年至2012年这一过程的示例

    library(rvest);library(tidyverse)
    
    
    scrape.draft = function(year){
    
      url = paste("https://www.eliteprospects.com/draft/nhl-entry-draft/",year,sep="")
    
      out = read_html(url) %>%
        html_table(header = T) %>% '[['(2) %>%
        filter(!grepl("ROUND",GP)) %>%
        mutate(draftYear = year)
    
      return(out)
    
    }
    
    temp = lapply(2010:2012,scrape.draft) %>%
      bind_rows()
    
    感谢您将这个死板的问题带到现实中:-)如果可能的话,请通过添加代码以用NA填充缺少的值来增强您建议的方法。除此之外,这个琐碎的问题—您提出的代码是有效的,a+1表示有效。感谢您解释代码工作流程。它将帮助其他可能认为你的答案同样有用的人。