-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
366 lines (345 loc) · 19.1 KB
/
index.html
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Scraping Resources</title>
<link rel="stylesheet" href="web_content/css/styles.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
</head>
<body>
<div class="container">
<h1>Scrape this website!</h1>
<p>Welcome! Are you interested in learning how to gather data from the internet? This website was designed as a trial ground to practice skills covered in Lorae Stojanovic's presentation to the Brookings Data Network on June 20, 2024, <a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html">"Web Scraping with Python"</a>. The presentation includes <a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html">slides</a>, <a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html">code to scrape this website</a>, and a <a href="https://github.com/lorae/web-scraping-tutorial">GitHub repository</a> encapsulating the entire project, including the webpage that you're currently reading.</p>
<p>The presentation covers foundational topics related to web scraping with Python, such as:</p>
<ul>
<li>How your browser <a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html#/2/5">interacts with external resources</a> to access and display a website</li>
<li><a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html#/4/2">Using the <code>requests</code> package</a> to access static web content via HTTP requests</li>
<li><a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html#/4/9">Parsing HTML code</a> using the <code>beautifulsoup4</code> package</li>
<li><a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html#/5">Accessing dynamic content</a> using the <code>selenium</code> package</li>
<li><a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html#/6/1">Inspecting network requests</a> to locate hidden APIs</li>
<li><a href="https://lorae.github.io/web-scraping-tutorial/advanced-web-scraping.slides.html#/6/10">Accessing APIs</a> using the <code>requests</code> package</li>
</ul>
<p>Feel free to explore the code accompanying the presentation, which scrapes data from this website using three methods:</p>
<ul>
<li><a href="#">HTTP requests and HTML parsing</a></li>
<li><a href="#">Selenium webdriver</a></li>
<li><a href="#">Direct API access</a></li>
</ul>
<p>Go forth and explore!</p>
<h2>Top US Companies by Profit per Employee</h2>
<p>Profit per employee is calculated by dividing a company's yearly profit by its number of full-time staff. Data are courtesy of the <a href="https://www.visualcapitalist.com/profit-per-employee-top-u-s-companies-ranking/">Visual Capitalist</a> and <a href="https://companiesmarketcap.com/">Companies Market Cap</a>.</p>
<table class="styled-table">
<thead>
<tr class="header-row">
<th class="header-cell rank">Rank</th>
<th class="header-cell company">Company</th>
<th class="header-cell industry">Industry</th>
<th class="header-cell profit-per-employee">Profit per Employee</th>
<th class="header-cell market-cap">Market Cap, June 2024</th>
</tr>
</thead>
<tbody>
<tr class="data-row">
<td class="data-cell rank">1</td>
<td class="data-cell company">ConocoPhillips</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$1,970,000</td>
<td class="data-cell market-cap">$127.38 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">2</td>
<td class="data-cell company">Fannie Mae</td>
<td class="data-cell industry">Financials</td>
<td class="data-cell profit-per-employee">$1,510,000</td>
<td class="data-cell market-cap">$5.16 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">3</td>
<td class="data-cell company">Freddie Mac</td>
<td class="data-cell industry">Financials</td>
<td class="data-cell profit-per-employee">$1,190,000</td>
<td class="data-cell market-cap">$22.71 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">4</td>
<td class="data-cell company">Valero</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$1,180,000</td>
<td class="data-cell market-cap">$49.39 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">5</td>
<td class="data-cell company">Occidental Petroleum</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$1,110,000</td>
<td class="data-cell market-cap">$54.31 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">6</td>
<td class="data-cell company">Cheniere Energy</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$921,000</td>
<td class="data-cell market-cap">$36.88 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">7</td>
<td class="data-cell company">ExxonMobil</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$899,000</td>
<td class="data-cell market-cap">$490.67 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">8</td>
<td class="data-cell company">Phillips 66</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$848,000</td>
<td class="data-cell market-cap">$57.59 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">9</td>
<td class="data-cell company">Marathon Petroleum</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$815,000</td>
<td class="data-cell market-cap">$60.75 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">10</td>
<td class="data-cell company">Chevron</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$809,000</td>
<td class="data-cell market-cap">$280.40 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">11</td>
<td class="data-cell company">PBF Energy</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$798,000</td>
<td class="data-cell market-cap">$5.10 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">12</td>
<td class="data-cell company">Enterprise Products</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$752,000</td>
<td class="data-cell market-cap">$61.45 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">13</td>
<td class="data-cell company">Apple</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$609,000</td>
<td class="data-cell market-cap">$3,245 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">14</td>
<td class="data-cell company">Broadcom</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$575,000</td>
<td class="data-cell market-cap">$839.05 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">15</td>
<td class="data-cell company">HF Sinclair</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$560,000</td>
<td class="data-cell market-cap">$10.01 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">16</td>
<td class="data-cell company">D. R. Horton</td>
<td class="data-cell industry">Construction</td>
<td class="data-cell profit-per-employee">$433,000</td>
<td class="data-cell market-cap">$45.90 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">17</td>
<td class="data-cell company">AIG</td>
<td class="data-cell industry">Financials</td>
<td class="data-cell profit-per-employee">$392,000</td>
<td class="data-cell market-cap">$49.19 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">18</td>
<td class="data-cell company">Lennar</td>
<td class="data-cell industry">Construction</td>
<td class="data-cell profit-per-employee">$384,000</td>
<td class="data-cell market-cap">$40.93 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">19</td>
<td class="data-cell company">Energy Transfer</td>
<td class="data-cell industry">Energy</td>
<td class="data-cell profit-per-employee">$379,000</td>
<td class="data-cell market-cap">$52.18 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">20</td>
<td class="data-cell company">Pfizer</td>
<td class="data-cell industry">Healthcare</td>
<td class="data-cell profit-per-employee">$378,000</td>
<td class="data-cell market-cap">$155.32 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">21</td>
<td class="data-cell company">Netflix</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$351,000</td>
<td class="data-cell market-cap">$295.45 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">22</td>
<td class="data-cell company">Microsoft</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$329,000</td>
<td class="data-cell market-cap">$3,317 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">23</td>
<td class="data-cell company">Alphabet</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$315,000</td>
<td class="data-cell market-cap">$2,170 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">24</td>
<td class="data-cell company">Meta</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$268,000</td>
<td class="data-cell market-cap">$1,266 B</td>
</tr>
<tr class="data-row">
<td class="data-cell rank">25</td>
<td class="data-cell company">Qualcomm</td>
<td class="data-cell industry">Tech</td>
<td class="data-cell profit-per-employee">$254,000</td>
<td class="data-cell market-cap">$253.43 B</td>
</tr>
</tbody>
</table>
<h2>Learning resources</h2>
<div class="promo-grid__promos" id="resources-container">
<!-- Data will be populated here -->
</div>
</div>
<script>
let resourcesData = [];
async function fetchResources() {
const response = await fetch('web_content/data/web-scraping-resources.json');
resourcesData = await response.json();
displayResources(resourcesData);
}
function displayResources(data) {
const container = document.getElementById('resources-container');
container.innerHTML = '';
data.forEach(resource => {
const card = document.createElement('div');
card.className = 'digest-card';
const topSection = document.createElement('div');
topSection.className = 'digest-card__top';
const image = document.createElement('img');
image.className = 'digest-card__image';
image.src = resource.image;
image.alt = `${resource.title} image`;
topSection.appendChild(image);
const textContainer = document.createElement('div');
textContainer.className = 'digest-card__text';
const title = document.createElement('div');
title.className = 'digest-card__title';
title.innerHTML = `<a href="${resource.link}">${resource.title}</a>`;
textContainer.appendChild(title);
const date = document.createElement('div');
date.className = 'digest-card__date';
date.innerHTML = `<span class="digest-card__label">${resource.date}</span>`;
textContainer.appendChild(date);
const authors = document.createElement('div');
authors.className = 'digest-card__items';
authors.innerHTML = `<span class="digest-card__label">Author(s) - </span>${resource.authors.map(author => `<span><a href="${author.link}">${author.name}</a></span>`).join(', ')}`;
textContainer.appendChild(authors);
topSection.appendChild(textContainer);
card.appendChild(topSection);
const description = document.createElement('div');
description.className = 'digest-card__summary';
description.textContent = resource.description;
card.appendChild(description);
const keywords = document.createElement('div');
keywords.className = 'digest-card__keywords';
keywords.innerHTML = `<span class="digest-card__label">Keywords: </span>${resource.keywords.join(', ')}`;
card.appendChild(keywords);
container.appendChild(card);
});
}
fetchResources();
// Function to fetch and display GDP data
async function fetchGDPData() {
const response = await fetch('web_content/data/gdp-data.csv');
const data = await response.text();
const parsedData = Papa.parse(data, { header: true }).data;
const labels = parsedData.map(row => row.Date);
const gdpValues = parsedData.map(row => parseFloat(row.GDP));
const ctx = document.getElementById('gdpChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'US GDP',
data: gdpValues,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
x: {
display: true,
title: {
display: true,
text: 'Year'
}
},
y: {
display: true,
title: {
display: true,
text: 'GDP (in billions)'
}
}
},
plugins: {
tooltip: {
enabled: true,
mode: 'nearest',
intersect: false,
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(context.parsed.y);
}
return label;
}
}
}
}
}
});
}
fetchGDPData();
</script>
<div class = "container">
<h2>Interactive Graph</h2>
<p>The following graph contains United States nominal Gross Domestic Product data from Q1 1947 to Q1 2024. Data is courtesy of the Federal Reserve Bank of St. Louis "FRED" service. </p>
<div class="container">
<canvas id="gdpChart" width="400" height="200"></canvas>
</div>
</div>
</body>
</html>