2016-11-17 47 views
0

我有一系列的9个网址,我想从刮数据:环与rvest

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 

偏移=在连杆的端部从0上升到900(当页面通过最后页面改变时,通过100)。我想循环遍历每个页面并刮擦每个表格,然后使用rbind将每个df按顺序堆叠在一起。我一直在使用rvest,并希望使用lapply,因为我比那更好的循环。

问题与此类似(Harvest (rvest) multiple HTML pages from a list of urls),但不同,因为我不希望在运行该程序之前将所有链接复制到一个向量。我想要一个通用的解决方案,以便如何遍历多个页面并收集数据,每次创建一个数据框。

第一页以下工作:

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

但我想对所有页面重复此操作而无需将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]] 

所以应该为每个页面的数据帧和我想象它会更容易把它们放在一个列表中,然后用rbind堆栈他们。

任何帮助将不胜感激!

+0

你管理的收获甚至第一页? – HubertL

+0

@HubertL是刚刚编辑上面的问题。第一块代码产生一个数据帧 – jvalenti

+1

这是另一个可能的解决方案:http://stackoverflow.com/questions/39129125/how-to-scrape-all-pages-1-2-3-n-from-a-网站使用r-vest/39131227#39131227 – Dave2e

回答

2

您正在尝试矢量化一个方法,该方法在一次调用中不能携带多个项目。具体而言,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 
+0

这是非常直观的,但我运行它时不断收到错误“下标超出范围”。 – jvalenti

+0

您在哪一行收到错误? 'dfList'是否填充? – Parfait

+0

它出现错误来自dfList的最后一行。 dfList不填充 – jvalenti

1

您可以使用curl一次运行所有请求。我对那些可能有小型服务器的网站很好,并且不要把它们炸毁。使用此代码,您可以在末尾使用lapply清理表格,以便与do.call(rbind, AllOut)堆叠,但我会将其保留给您。

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

OffSet <- seq(0, 900, by = 100) 

Sites <- paste0('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=', OffSet) 


library(curl) 

out <<- list() 
# This is function, function which will be run if data vendor call is successful 
complete = function(res){ 
    # cat("Request done! Status:", res$status, "\n") 
    out <<- c(out, list(res)) 
} 

for(i in 1:length(Sites)){ 
    curl_fetch_multi(
    Sites[i] 
    , done = complete 
    , fail = print 
    , handle = new_handle(customrequest = "GET") 
    ) 
} 

multi_run() 

AllOut <- lapply(out, function(x){ 

    webpage <- read_html(x$content) 
    draft_table <- html_nodes(webpage, 'table') 
    Tab <- html_table(draft_table) 
    if(length(Tab) == 0){ 
    NULL 
    } else { 
    Tab 
    } 

})