使用 rvest 在 r 中循环多个 url

jva*_*nti 3 html url r web-scraping rvest

我有一系列 9 个网址,我想从中抓取数据:

http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=0 
Run Code Online (Sandbox Code Playgroud)

当页面更改到最后一页时,链接末尾的 offset= 从 0 到 900(乘以 100)。我想遍历每个页面并抓取每个表,然后使用 rbind 将每个 df 按顺序堆叠在一起。我一直在使用 rvest 并且想使用 lapply 因为我比 for 循环更好。

问题与此类似(从 url 列表中收获 (rvest) 多个 HTML 页面)但不同,因为我不想在运行程序之前将所有链接复制到一个向量。我想要一个关于如何遍历多个页面并收集数据的通用解决方案,每次创建一个数据框。

以下适用于第一页:

library(rvest)
library(stringr)
library(tidyr)

site <- 'http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=0' 

webpage <- read_html(site)
draft_table <- html_nodes(webpage, 'table')
draft <- html_table(draft_table)[[1]]
Run Code Online (Sandbox Code Playgroud)

但我想在所有页面上重复这一点,而不必将 url 粘贴到向量中。我尝试了以下方法,但没有奏效:

jump <- seq(0, 900, by = 100)
site <- paste('http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=', jump,'.htm', sep="")

webpage <- read_html(site)
draft_table <- html_nodes(webpage, 'table')
draft <- html_table(draft_table)[[1]]
Run Code Online (Sandbox Code Playgroud)

所以每个页面都应该有一个数据框,我想把它们放在一个列表中然后使用 rbind 来堆叠它们会更容易。

任何帮助将不胜感激!

Par*_*ait 6

您正在尝试对无法在一次调用中获取多个项目的方法进行矢量化。具体来说,read_html()每次调用需要一页,因为它需要一次读取一个网络数据并期望一个标量值。考虑循环遍历site列表,lapply然后将所有 dfs 绑定在一起:

jump <- seq(0, 800, by = 100)
site <- paste('http://www.basketball-reference.com/play-index/draft_finder.cgi?',
              'request=1&year_min=2001&year_max=2014&round_min=&round_max=',
              '&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0',
              '&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y',
              '&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=',
              '&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id',
              '&order_by_asc=&offset=', jump, sep="")

dfList <- lapply(site, function(i) {
    webpage <- read_html(i)
    draft_table <- html_nodes(webpage, 'table')
    draft <- html_table(draft_table)[[1]]
})

finaldf <- do.call(rbind, dfList)             # ASSUMING ALL DFs MAINTAIN SAME COLS
Run Code Online (Sandbox Code Playgroud)