-
Notifications
You must be signed in to change notification settings - Fork 0
/
VOC_wunderground_data.Rmd
351 lines (342 loc) · 12.9 KB
/
VOC_wunderground_data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
---
title: "Weather Underground Web Scraper"
author: "Lourdes Vera"
date: "2/13/2021"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
#each line installs a package that will be needed to scrape the data
#install.packages('tidyverse')
#install.packages('rvest')
#install.packages('datetime')
#install.packages('vctrs')
```
```{r}
#Load the packages you installed
library(tidyverse)
library(datetime)
library(rvest)
library(vctrs)
```
#Create Dataframe of Dates
```{r}
#this is the list of days where you sampled data
data <- as.data.frame(c("2020-02-23", "2020-02-24", "2020-02-25", "2020-02-26", "2020-02-27", "2020-02-28", "2020-02-29", "2020-03-01", "2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05", "2020-03-06", "2020-03-07", "2020-03-08", "2020-03-09","2020-03-10","2020-03-11","2020-03-12","2020-03-13","2020-03-14","2020-03-15","2020-03-16","2020-03-17","2020-03-18","2020-03-19","2020-03-20","2020-03-21","2020-03-22","2020-03-23","2020-03-24","2020-03-25","2020-03-26","2020-03-27","2020-03-28","2020-03-29","2020-03-30","2020-03-31","2020-04-01","2020-04-02","2020-04-03","2020-04-04","2020-04-05", "2020-04-06", "2020-04-07", "2020-04-08", "2020-04-09", "2020-04-10", "2020-04-11", "2020-04-12", "2020-04-13", "2020-04-14", "2020-04-15", "2020-04-16", "2020-04-17", "2020-04-18", "2020-04-19", "2020-04-20", "2020-04-21", "2020-04-22", "2020-04-23", "2020-04-24","2020-04-25", "2020-04-26", "2020-04-27", "2020-04-28", "2020-04-29", "2020-04-30", "2020-05-01", "2020-05-02", "2020-05-03", "2020-05-04"))
```
#Generate URLS
```{r}
#Paste each data into the URL to get a URL that directly takes you to the webpage for each day
urls<-NA
for (i in 1:nrow(data)){
urls[i] <- paste0("https://www.wunderground.com/dashboard/pws/KTXKARNE7/table/",data[i,1],"/",data[i,1],"/daily")
}
urls
```
#Scrape Data from Wunderground (takes a long time)
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 1:10){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 11:20){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 21:30){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 31:40){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 41:50){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 51:60){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
```{r}
#This is the loop that goes through each URL listed above to scrape the weather data from the page
for(i in 61:72){ #the number of URLs
assign(paste0("DF", data[i,1]), urls[i] %>%
read_html() %>%
html_nodes(xpath='/html/body/app-root/app-dashboard/one-column-layout/wu-header/sidenav/mat-sidenav-container/mat-sidenav-content/div/section/section[1]/div[1]/div/section/div/div/div/lib-history/div[2]/lib-history-table/div/div/div/table')
%>%
html_table(fill=TRUE))
}
```
#Save Each Date as a Dataframe
```{r}
#Assign each set of data from the webpage to a data frame
'DF2020-02-23' <- as.data.frame(`DF2020-02-23`)
'DF2020-02-24' <- as.data.frame(`DF2020-02-24`)
'DF2020-02-25' <- as.data.frame(`DF2020-02-25`)
'DF2020-02-26' <- as.data.frame(`DF2020-02-26`)
'DF2020-02-27' <- as.data.frame(`DF2020-02-27`)
'DF2020-02-28' <- as.data.frame(`DF2020-02-28`)
'DF2020-02-29' <- as.data.frame(`DF2020-02-29`)
'DF2020-03-01' <- as.data.frame(`DF2020-03-01`)
'DF2020-03-02' <- as.data.frame(`DF2020-03-02`)
'DF2020-03-03' <- as.data.frame(`DF2020-03-03`)
'DF2020-03-04' <- as.data.frame(`DF2020-03-04`)
'DF2020-03-05' <- as.data.frame(`DF2020-03-05`)
'DF2020-03-06' <- as.data.frame(`DF2020-03-06`)
'DF2020-03-07' <- as.data.frame(`DF2020-03-07`)
'DF2020-03-08' <- as.data.frame(`DF2020-03-08`)
'DF2020-03-09' <- as.data.frame(`DF2020-03-09`)
'DF2020-03-10' <- as.data.frame(`DF2020-03-10`)
'DF2020-03-11' <- as.data.frame(`DF2020-03-11`)
'DF2020-03-12' <- as.data.frame(`DF2020-03-12`)
'DF2020-03-13' <- as.data.frame(`DF2020-03-13`)
'DF2020-03-14' <- as.data.frame(`DF2020-03-14`)
'DF2020-03-15' <- as.data.frame(`DF2020-03-15`)
'DF2020-03-16' <- as.data.frame(`DF2020-03-16`)
'DF2020-03-17' <- as.data.frame(`DF2020-03-17`)
'DF2020-03-18' <- as.data.frame(`DF2020-03-18`)
'DF2020-03-19' <- as.data.frame(`DF2020-03-19`)
'DF2020-03-20' <- as.data.frame(`DF2020-03-20`)
'DF2020-03-21' <- as.data.frame(`DF2020-03-21`)
'DF2020-03-22' <- as.data.frame(`DF2020-03-22`)
'DF2020-03-23' <- as.data.frame(`DF2020-03-23`)
'DF2020-03-24' <- as.data.frame(`DF2020-03-24`)
'DF2020-03-25' <- as.data.frame(`DF2020-03-25`)
'DF2020-03-26' <- as.data.frame(`DF2020-03-26`)
'DF2020-03-27' <- as.data.frame(`DF2020-03-27`)
'DF2020-03-28' <- as.data.frame(`DF2020-03-28`)
'DF2020-03-29' <- as.data.frame(`DF2020-03-29`)
'DF2020-03-30' <- as.data.frame(`DF2020-03-30`)
'DF2020-03-31' <- as.data.frame(`DF2020-03-31`)
'DF2020-04-01' <- as.data.frame(`DF2020-04-01`)
'DF2020-04-02' <- as.data.frame(`DF2020-04-02`)
'DF2020-04-03' <- as.data.frame(`DF2020-04-03`)
'DF2020-04-04' <- as.data.frame(`DF2020-04-04`)
'DF2020-04-05' <- as.data.frame(`DF2020-04-05`)
'DF2020-04-06' <- as.data.frame(`DF2020-04-06`)
'DF2020-04-07' <- as.data.frame(`DF2020-04-07`)
'DF2020-04-08' <- as.data.frame(`DF2020-04-08`)
'DF2020-04-09' <- as.data.frame(`DF2020-04-09`)
'DF2020-04-10' <- as.data.frame(`DF2020-04-10`)
'DF2020-04-11' <- as.data.frame(`DF2020-04-11`)
'DF2020-04-12' <- as.data.frame(`DF2020-04-12`)
'DF2020-04-13' <- as.data.frame(`DF2020-04-13`)
'DF2020-04-14' <- as.data.frame(`DF2020-04-14`)
'DF2020-04-15' <- as.data.frame(`DF2020-04-15`)
'DF2020-04-16' <- as.data.frame(`DF2020-04-16`)
'DF2020-04-17' <- as.data.frame(`DF2020-04-17`)
'DF2020-04-18' <- as.data.frame(`DF2020-04-18`)
'DF2020-04-19' <- as.data.frame(`DF2020-04-19`)
'DF2020-04-20' <- as.data.frame(`DF2020-04-20`)
'DF2020-04-21' <- as.data.frame(`DF2020-04-21`)
'DF2020-04-22' <- as.data.frame(`DF2020-04-22`)
'DF2020-04-23' <- as.data.frame(`DF2020-04-23`)
'DF2020-04-24' <- as.data.frame(`DF2020-04-24`)
'DF2020-04-25' <- as.data.frame(`DF2020-04-25`)
'DF2020-04-26' <- as.data.frame(`DF2020-04-26`)
'DF2020-04-27' <- as.data.frame(`DF2020-04-27`)
'DF2020-04-28' <- as.data.frame(`DF2020-04-28`)
'DF2020-04-29' <- as.data.frame(`DF2020-04-29`)
'DF2020-04-30' <- as.data.frame(`DF2020-04-30`)
'DF2020-05-01' <- as.data.frame(`DF2020-05-01`)
'DF2020-05-02' <- as.data.frame(`DF2020-05-02`)
'DF2020-05-03' <- as.data.frame(`DF2020-05-03`)
'DF2020-05-04' <- as.data.frame(`DF2020-05-04`)
```
```{r}
#add dates as data frame columns
`DF2020-02-23`$date <- "2020-02-23"
`DF2020-02-24`$date <- "2020-02-24"
`DF2020-02-25`$date <- "2020-02-25"
`DF2020-02-26`$date <- "2020-02-26"
`DF2020-02-27`$date <- "2020-02-27"
`DF2020-02-28`$date <- "2020-02-28"
`DF2020-02-29`$date <- "2020-02-29"
`DF2020-03-01`$date <- "2020-03-01"
`DF2020-03-02`$date <- "2020-03-02"
`DF2020-03-03`$date <- "2020-03-03"
`DF2020-03-04`$date <- "2020-03-04"
`DF2020-03-05`$date <- "2020-03-05"
`DF2020-03-06`$date <- "2020-03-06"
`DF2020-03-07`$date <- "2020-03-07"
`DF2020-03-08`$date <- "2020-03-08"
`DF2020-03-09`$date <- "2020-03-09"
`DF2020-03-10`$date <- "2020-03-10"
`DF2020-03-11`$date <- "2020-03-11"
`DF2020-03-12`$date <- "2020-03-12"
`DF2020-03-13`$date <- "2020-03-13"
`DF2020-03-14`$date <- "2020-03-14"
`DF2020-03-15`$date <- "2020-03-15"
`DF2020-03-16`$date <- "2020-03-16"
`DF2020-03-17`$date <- "2020-03-17"
`DF2020-03-18`$date <- "2020-03-18"
`DF2020-03-19`$date <- "2020-03-19"
`DF2020-03-20`$date <- "2020-03-20"
`DF2020-03-21`$date <- "2020-03-21"
`DF2020-03-22`$date <- "2020-03-22"
`DF2020-03-23`$date <- "2020-03-23"
`DF2020-03-24`$date <- "2020-03-24"
`DF2020-03-25`$date <- "2020-03-25"
`DF2020-03-26`$date <- "2020-03-26"
`DF2020-03-27`$date <- "2020-03-27"
`DF2020-03-28`$date <- "2020-03-28"
`DF2020-03-29`$date <- "2020-03-29"
`DF2020-03-30`$date <- "2020-03-30"
`DF2020-03-31`$date <- "2020-03-31"
`DF2020-04-01`$date <- "2020-04-01"
`DF2020-04-02`$date <- "2020-04-02"
`DF2020-04-03`$date <- "2020-04-03"
`DF2020-04-04`$date <- "2020-04-04"
`DF2020-04-05`$date <- "2020-04-05"
`DF2020-04-06`$date <- "2020-04-06"
`DF2020-04-07`$date <- "2020-04-07"
`DF2020-04-08`$date <- "2020-04-08"
`DF2020-04-09`$date <- "2020-04-09"
`DF2020-04-10`$date <- "2020-04-10"
`DF2020-04-11`$date <- "2020-04-11"
`DF2020-04-12`$date <- "2020-04-12"
`DF2020-04-13`$date <- "2020-04-13"
`DF2020-04-14`$date <- "2020-04-14"
`DF2020-04-15`$date <- "2020-04-15"
`DF2020-04-16`$date <- "2020-04-16"
`DF2020-04-17`$date <- "2020-04-17"
`DF2020-04-18`$date <- "2020-04-18"
`DF2020-04-19`$date <- "2020-04-19"
`DF2020-04-20`$date <- "2020-04-20"
`DF2020-04-21`$date <- "2020-04-21"
`DF2020-04-22`$date <- "2020-04-22"
`DF2020-04-23`$date <- "2020-04-23"
`DF2020-04-24`$date <- "2020-04-24"
`DF2020-04-25`$date <- "2020-04-25"
`DF2020-04-26`$date <- "2020-04-26"
`DF2020-04-27`$date <- "2020-04-27"
`DF2020-04-28`$date <- "2020-04-28"
`DF2020-04-29`$date <- "2020-04-29"
`DF2020-04-30`$date <- "2020-04-30"
`DF2020-05-01`$date <- "2020-05-01"
`DF2020-05-02`$date <- "2020-05-02"
`DF2020-05-03`$date <- "2020-05-03"
`DF2020-05-04`$date <- "2020-05-04"
```
#Build the main dataframe
```{r}
#combine the data frames together
DF_All <- vctrs::vec_c(`DF2020-02-23`,
`DF2020-02-24`,
`DF2020-02-25`,
`DF2020-02-26`,
`DF2020-02-27`,
`DF2020-02-28`,
`DF2020-02-29`,
`DF2020-03-01`,
`DF2020-03-02`,
`DF2020-03-03`,
`DF2020-03-04`,
`DF2020-03-05`,
`DF2020-03-06`,
`DF2020-03-07`,
`DF2020-03-08`,
`DF2020-03-09`,
`DF2020-03-10`,
`DF2020-03-11`,
`DF2020-03-12`,
`DF2020-03-13`,
`DF2020-03-14`,
`DF2020-03-15`,
`DF2020-03-16`,
`DF2020-03-17`,
`DF2020-03-18`,
`DF2020-03-19`,
`DF2020-03-20`,
`DF2020-03-21`,
`DF2020-03-22`,
`DF2020-03-23`,
`DF2020-03-24`,
`DF2020-03-25`,
`DF2020-03-26`,
`DF2020-03-27`,
`DF2020-03-28`,
`DF2020-03-29`,
`DF2020-03-30`,
`DF2020-03-31`,
`DF2020-04-01`,
`DF2020-04-02`,
`DF2020-04-03`,
`DF2020-04-04`,
`DF2020-04-05`,
`DF2020-04-06`,
`DF2020-04-07`,
`DF2020-04-08`,
`DF2020-04-09`,
`DF2020-04-10`,
`DF2020-04-11`,
`DF2020-04-12`,
`DF2020-04-13`,
`DF2020-04-14`,
`DF2020-04-15`,
`DF2020-04-16`,
`DF2020-04-17`,
`DF2020-04-18`,
`DF2020-04-19`,
`DF2020-04-20`,
`DF2020-04-21`,
`DF2020-04-22`,
`DF2020-04-23`,
`DF2020-04-24`,
`DF2020-04-25`,
`DF2020-04-26`,
`DF2020-04-27`,
`DF2020-04-28`,
`DF2020-04-29`,
`DF2020-04-30`,
`DF2020-05-01`,
`DF2020-05-02`,
`DF2020-05-03`,
`DF2020-05-04`)
```
```{r}
#check out your new data frame. Make sure the dates are in there.
DF_All
```
```{r}
write.csv(DF_All, "DF_All.csv")
```