我有一系列的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堆栈他们。
任何帮助将不胜感激!
你管理的收获甚至第一页? – HubertL
@HubertL是刚刚编辑上面的问题。第一块代码产生一个数据帧 – jvalenti
这是另一个可能的解决方案:http://stackoverflow.com/questions/39129125/how-to-scrape-all-pages-1-2-3-n-from-a-网站使用r-vest/39131227#39131227 – Dave2e