Post

Webscraping

I learned how to use Javascript to scrape flight data.

I’ve been wanting to learn how to get better at webscraping and javascript so today I wanted try to write a program to webscrape in javascript.

My goal was to do a simple thing: given a start location and date and end location and date, retrieve details (price, departure time, arrival time, duration of flight, airline, etc) about available flights for that date.

It wasn’t as hard as I thought it would be. In this example, I scraped kayak.com for flights from Washington DC to Los Angeles, dearting on 2024-11-12 and returning on 2024-11-19. 1

Code

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
import puppeteer from "puppeteer";

async function get_flights(start, end, from, to) {
    const url = `https://www.kayak.com/flights/${start}-${end}/${from}/${to}?sort=bestflight_a`;
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
    });
    const page = await browser.newPage();
    await page.goto(url, {
        waitUntil: "domcontentloaded",
    });

    const filght_info = await page.evaluate(() => {
        let ret = [];
        document.querySelectorAll(".nrc6").forEach(trip => {
            let cur_trip = {};
            cur_trip.legs = [];
            trip.querySelectorAll(".hJSA-item").forEach(leg => {
                try {
                    const airline = leg.querySelector(".c_cgF").innerText;
                    const times = leg.querySelector(".VY2U").querySelectorAll("span");
                    const departure_time = times[0].innerText;
                    const arrival_time = times[2].innerText;
                    const airports = leg.querySelectorAll(".jLhY-airport-info");
                    const from_airport = airports[0].innerText;
                    const to_airport = airports[1].innerText;
                    const duration = leg.querySelector(".xdW8").querySelector(".vmXl").innerText;
                    cur_trip.legs.push({
                        airline: airline,
                        departure_time: departure_time,
                        arrival_time: arrival_time,
                        from_airport: from_airport,
                        to_airport: to_airport,
                        duration: duration,
                    });
                } catch (error) {
                    console.error("Error processing leg: ", error);
                }
            });
            ret.push(cur_trip);
        });
        return ret;
    });
    await browser.close();
    return filght_info;
}

const flights = await get_flights("WAS", "LAX", "2024-11-12", "2024-11-19");
console.log(JSON.stringify(flights));

Output

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
[
  {
    "legs": [
      {
        "airline": "Spirit Airlines",
        "departure_time": "6:30 am",
        "arrival_time": "9:27 am",
        "from_airport": "BWI",
        "to_airport": "LAX",
        "duration": "5h 57m"
      },
      {
        "airline": "Spirit Airlines",
        "departure_time": "12:55 am",
        "arrival_time": "8:43 am",
        "from_airport": "LAX",
        "to_airport": "BWI",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "Spirit Airlines",
        "departure_time": "6:30 am",
        "arrival_time": "9:27 am",
        "from_airport": "BWI",
        "to_airport": "LAX",
        "duration": "5h 57m"
      },
      {
        "airline": "Spirit Airlines",
        "departure_time": "12:55 am",
        "arrival_time": "8:43 am",
        "from_airport": "LAX",
        "to_airport": "BWI",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "Alaska Airlines",
        "departure_time": "5:28 pm",
        "arrival_time": "8:10 pm",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 42m"
      },
      {
        "airline": "Spirit Airlines",
        "departure_time": "12:55 am",
        "arrival_time": "8:43 am",
        "from_airport": "LAX",
        "to_airport": "BWI",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "9:00 am",
        "arrival_time": "11:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "10:45 pm",
        "arrival_time": "6:32 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 47m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "6:00 am",
        "arrival_time": "8:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "10:45 pm",
        "arrival_time": "6:32 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 47m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "6:00 am",
        "arrival_time": "8:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "11:15 pm",
        "arrival_time": "7:03 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "9:00 am",
        "arrival_time": "11:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "4:55 pm",
        "arrival_time": "12:43 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "6:00 am",
        "arrival_time": "8:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "4:55 pm",
        "arrival_time": "12:43 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "9:00 am",
        "arrival_time": "11:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "11:15 pm",
        "arrival_time": "7:03 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "9:00 am",
        "arrival_time": "11:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "9:15 pm",
        "arrival_time": "5:03 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "6:00 am",
        "arrival_time": "8:49 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "9:15 pm",
        "arrival_time": "5:03 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "10:10 pm",
        "arrival_time": "12:59 am+1",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 49m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "4:55 pm",
        "arrival_time": "12:43 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "7:05 pm",
        "arrival_time": "9:55 pm",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 50m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "10:45 pm",
        "arrival_time": "6:32 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 47m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "8:15 am",
        "arrival_time": "11:05 am",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 50m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "10:45 pm",
        "arrival_time": "6:32 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 47m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "7:05 pm",
        "arrival_time": "9:55 pm",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 50m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "9:15 pm",
        "arrival_time": "5:03 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  },
  {
    "legs": [
      {
        "airline": "United Airlines",
        "departure_time": "7:05 pm",
        "arrival_time": "9:55 pm",
        "from_airport": "IAD",
        "to_airport": "LAX",
        "duration": "5h 50m"
      },
      {
        "airline": "United Airlines",
        "departure_time": "11:15 pm",
        "arrival_time": "7:03 am+1",
        "from_airport": "LAX",
        "to_airport": "IAD",
        "duration": "4h 48m"
      }
    ]
  }
]

  1. This article was a helpful guide. ↩︎

This post is licensed under CC BY 4.0 by the author.