【问题标题】:How to scrape data from multiple pages by dynamically updating the url with rvest如何通过使用 rvest 动态更新 url 从多个页面中抓取数据
【发布时间】:2018-10-05 09:04:00
【问题描述】:

我正在尝试从此website 中提取数据。我有兴趣从draft selections by year 中提取数据。年份范围从 1963 年到 2018 年。
url 结构有一个共同的模式。比如它的https://www.eliteprospects.com/draft/nhl-entry-draft/2018https://www.eliteprospects.com/draft/nhl-entry-draft/2017等等。

到目前为止,我已经成功提取了一年的数据。我编写了一个自定义函数,其中,给定输入,刮板将收集数据并以漂亮的数据帧格式呈现。

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...

问题:如何编写代码使网页 url 中的年份自动递增,从而使爬虫能够提取相关数据并写入数据框。?

注意:我已经查看了一些相关问题,例如1234,但找不到我的解决方案。

【问题讨论】:

    标签: web-scraping rvest


    【解决方案1】:

    我将创建一个针对给定年份进行抓取的函数,然后绑定该年份的行。

    1. 使用paste() 使用字符串和年份变量创建动态网址
    2. 为 url 编写抓取函数(注意:您不必使用 html_text - 它存储为表格,因此可以使用 html_table() 直接提取)李>
    3. 使用lapply() 循环函数多年
    4. 使用bind_rows()组合列表中的dfs

    以下是 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()
    

    【讨论】:

    • 感谢您将这个死问题变为现实 :-) 如果可能,请进一步增强您提出的方法,添加代码以填充缺失值不适用。此外,这个微不足道的问题,您提出的代码有效,为它 +1。并感谢您解释代码工作流程。它会帮助其他可能发现您的答案同样有用的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    相关资源
    最近更新 更多