我正在尝试通过为每个日期运行带有迭代 [i] 的 for 循环,将地下天气的历史数据下载为 csv 格式。
为了加快运行时间,我从 2019 年 9 月 1 日到今天运行代码(实际上我需要运行几年)。我正在为 url 运行一个 for 循环:
myurl[i] <- paste("https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=",station,"&day=",my_dates$day[i],"&month=",my_dates$month[i],"&year=",my_dates$year[i],"&graphspan=day&format=1", sep = "")
其中 i 是开始和结束之间的日期。例如。 https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IISLEOFW4&day=1&month=9&year=2019&graphspan=day&format=1
start_date <- as.POSIXct("2019-09-01",tz="gmt")
end_date <- as.POSIXct(Sys.Date(),tz="gmt")
dates <- seq.POSIXt(start_date,end_date,by="day")
my_dates<-as.integer(unlist(strsplit(as.character(dates),"-")))
my_dates<-array(my_dates,dim=c(3,length(my_dates)/3))
my_dates<-as.data.frame(t(my_dates), )
colnames(my_dates)<-c("year","month","day")
station <- "IISLEOFW4"
for(i in 1:length(my_dates)) {
myurl[i] <- paste("https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=",station,"&day=",my_dates$day[i],"&month=",my_dates$month[i],"&year=",my_dates$year[i],"&graphspan=day&format=1", sep = "")
folder <- "D:\\WeatherData"
myfile <- paste("NewportWeather",rownames(my_dates),".csv")
download.file(url = myurl[i], file.path(folder, myfile, fsep = "\\" ))
}
运行此程序时,只有一天的数据会下载到正确的文件位置,而不是日期范围内的所有数据。是否有人能够帮助我将整个日期范围内的所有数据下载到单独的文件中,或者全部附加到同一个文件中。
您需要更改文件下载到的文件夹才能运行代码。
感谢您的帮助!