我正在尝试使用循环抓取Web数据,并将每个实例写入同一excel文件中的不同工作表。我已经包含了下面的脚本。工作表名称已正确创建,但相同的数据框将写入两个工作表。真的很感激任何帮助。谢谢!
library(httr)
library(jsonlite)
library(tidyverse)
options(width=120)
xyz=c('"strStartDate": "2016-9-25", "strEndDate": "2016-11-1"','"strStartDate": "2016-8-25", "strEndDate": "2016-9-1"')
abc=c("2016-9-25","2016-8-25")
for (i in xyz){
for(j in abc){
ret <- fromJSON(content(POST("http://www.fangraphs.com/splitstool.aspx/getsplitleaders",
content_type_json(),
body = paste0('{"strPlayerId": "all", "strSplitArr": "[]", "strGroup": "season", "strPosition": "B", "strType": "1",',i,',"strSplitTeams": "false", "dctFilters": [], "strStatType": "player", "strAutoPt": "true"}')), as="text"))
df <- setNames(as_data_frame(ret$d$v), ret$d$k)
write.xlsx(df,"Slas.xlsx",append=TRUE,sheetName=j)}}
答案 0 :(得分:1)
req.df<-data.frame(strStartDate = c("2016-9-25", "2016-8-25"),
strEndDate = c("2016-11-1", "2016-9-1"), stringsAsFactors = FALSE)
for(i in 1:nrow(req.df)){
ret<- fromJSON(content(POST("http://www.fangraphs.com/splitstool.aspx/getsplitleaders",
content_type_json(),
body = paste0('{"strPlayerId": "all", "strSplitArr": "[]", "strGroup": "season", "strPosition": "B", "strType": "1",',
'"strStartDate": "', req.df[i,"strStartDate"] ,'", "strEndDate":"', req.df[i,"strEndDate"],'"',
',"strSplitTeams": "false", "dctFilters": [], "strStatType": "player", "strAutoPt": "true"}')), as="text"))
df <- setNames(as_data_frame(ret$d$v), ret$d$k)
write.xlsx(df,"Slas.xlsx",append=TRUE,sheetName=req.df[i,"strStartDate"])
}