-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_people.py
More file actions
354 lines (291 loc) · 12.1 KB
/
extract_people.py
File metadata and controls
354 lines (291 loc) · 12.1 KB
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
#!/usr/bin/env python3
"""Extract people data from existing HTML into Excel spreadsheet.
This is a one-time script to migrate existing HTML data to the spreadsheet format.
"""
import re
from pathlib import Path
from bs4 import BeautifulSoup
import openpyxl
def extract_people(html_path: Path) -> dict:
"""Extract all people data from HTML file.
Returns dict with keys: director, members, alumni_postdocs, alumni_grads,
alumni_managers, alumni_undergrads, collaborators
"""
with open(html_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
data = {}
# Extract lab director
director_section = soup.find('div', class_='lab-director')
if director_section:
img = director_section.find('img')
h3 = director_section.find('h3')
ps = director_section.find_all('p')
name_parts = h3.get_text().split('|') if h3 else ['', '']
name = name_parts[0].strip() if name_parts else ''
role = name_parts[1].strip() if len(name_parts) > 1 else ''
data['director'] = [{
'image': img.get('src', '').replace('images/people/', '') if img else '',
'name': name,
'role': role,
'bio': ps[0].get_text(strip=True) if ps else '',
'links_html': get_inner_html(ps[1]) if len(ps) > 1 else '',
'name_url': ''
}]
print(f"Extracted director: {name}")
# Extract active lab members
members = []
people_grids = soup.find_all('div', class_='people-grid')
for grid in people_grids:
cards = grid.find_all('div', class_='person-card')
for card in cards:
member = extract_person_card(card)
members.append(member)
data['members'] = members
print(f"Extracted {len(members)} active lab members")
# Extract alumni (from the lab-alumni section)
alumni_section = soup.find('section', id='lab-alumni')
if alumni_section:
# Find all h3 headers that identify categories
h3s = alumni_section.find_all('h3')
for h3 in h3s:
header_text = h3.get_text(strip=True).lower()
next_elem = h3.find_next_sibling()
# Skip non-alumni headers
if 'who we were' in header_text or 'fledgling' in header_text.lower():
continue
if 'postdoctoral' in header_text:
data['alumni_postdocs'] = extract_alumni_list(next_elem)
print(f"Extracted {len(data['alumni_postdocs'])} postdocs")
elif 'undergraduate' in header_text:
# Check undergraduate BEFORE graduate since 'undergraduate' contains 'graduate'
alumni_list = next_elem
if alumni_list and alumni_list.name == 'p':
data['alumni_undergrads'] = extract_alumni_simple_list(alumni_list)
else:
data['alumni_undergrads'] = []
print(f"Extracted {len(data.get('alumni_undergrads', []))} undergrad alumni")
elif 'graduate' in header_text:
data['alumni_grads'] = extract_alumni_list(next_elem)
print(f"Extracted {len(data['alumni_grads'])} grad students")
elif 'manager' in header_text:
data['alumni_managers'] = extract_alumni_list(next_elem)
print(f"Extracted {len(data['alumni_managers'])} lab managers")
# Extract collaborators
collab_section = soup.find('section', id='collaborators')
if collab_section:
collaborators = []
# Find all paragraphs with links
for p in collab_section.find_all('p'):
link = p.find('a')
if link and p.get_text(strip=True):
collab = {
'name': link.get_text(strip=True),
'url': link.get('href', ''),
'description': p.get_text(strip=True)
}
collaborators.append(collab)
data['collaborators'] = collaborators
print(f"Extracted {len(collaborators)} collaborators")
return data
def extract_person_card(card) -> dict:
"""Extract data from a person card."""
img = card.find('img')
h3 = card.find('h3')
p = card.find('p')
# Parse name and role from h3
name = ''
role = ''
name_url = ''
if h3:
link = h3.find('a')
if link:
name_url = link.get('href', '')
text = h3.get_text()
if '|' in text:
parts = text.split('|')
name = parts[0].strip()
role = parts[1].strip()
else:
name = text.strip()
return {
'image': img.get('src', '').replace('images/people/', '') if img else '',
'name': name,
'name_url': name_url,
'role': role,
'bio': p.get_text(strip=True) if p else '',
'links_html': ''
}
def extract_alumni_list(elem) -> list:
"""Extract alumni from a paragraph with <br> separated entries.
Handles formats like:
- Gina Notaro (2017-2018; now at <a>Lockheed Martin</a>)
- <a>Andrew Heusser</a> (2016-2018; now at <a>Akili</a>)
"""
alumni = []
if not elem:
return alumni
# Get the raw HTML and split by <br>
html = str(elem)
entries = re.split(r'<br\s*/?>', html)
for entry in entries:
entry = entry.strip()
if not entry:
continue
# Remove opening/closing <p> tags but keep the content
entry = re.sub(r'^<p[^>]*>', '', entry)
entry = re.sub(r'</p>$', '', entry)
entry = entry.strip()
if not entry:
continue
entry_soup = BeautifulSoup(entry, 'html.parser')
full_text = entry_soup.get_text(strip=True)
if not full_text:
continue
alum = {
'name': '',
'name_url': '',
'years': '',
'current_position': '',
'current_position_url': ''
}
# Find all links in this entry
links = entry_soup.find_all('a')
# The first link (if any) that appears BEFORE the parenthesis is the person's name
# Links inside parenthesis are their current position
paren_pos = full_text.find('(')
# Determine which link is the name vs current position
name_link = None
position_link = None
for link in links:
link_text = link.get_text(strip=True)
link_pos_in_text = full_text.find(link_text)
if paren_pos < 0 or link_pos_in_text < paren_pos:
# Link is before parenthesis - this is the name
if name_link is None:
name_link = link
else:
# Link is inside parenthesis - this is the current position
position_link = link
# Get name - either from link or from text before parenthesis
if name_link:
alum['name'] = name_link.get_text(strip=True)
alum['name_url'] = name_link.get('href', '')
else:
# No name link, name is text before (
if paren_pos > 0:
alum['name'] = full_text[:paren_pos].strip()
else:
alum['name'] = full_text.strip()
# Get position link URL if available
if position_link:
alum['current_position_url'] = position_link.get('href', '')
# Parse years and current position from parenthesis
paren_match = re.search(r'\(([^)]+)\)', full_text)
if paren_match:
paren_content = paren_match.group(1)
if ';' in paren_content:
parts = paren_content.split(';', 1)
alum['years'] = parts[0].strip()
# Clean up spacing issues from stripped links
position = parts[1].strip()
position = re.sub(r'(now|then)\s+(at?)\s*', r'\1 \2 ', position)
alum['current_position'] = position
else:
alum['years'] = paren_content.strip()
if alum['name']:
alumni.append(alum)
return alumni
def extract_alumni_simple_list(elem) -> list:
"""Extract simple alumni list (just names and years)."""
alumni = []
if not elem:
return alumni
# Get text and split by <br>
html = str(elem)
entries = re.split(r'<br\s*/?>', html)
for entry in entries:
entry_soup = BeautifulSoup(entry, 'html.parser')
text = entry_soup.get_text(strip=True)
if not text:
continue
# Format: "Name (years)"
match = re.match(r'(.+?)\s*\(([^)]+)\)', text)
if match:
alumni.append({
'name': match.group(1).strip(),
'years': match.group(2).strip()
})
else:
alumni.append({
'name': text.strip(),
'years': ''
})
return alumni
def get_inner_html(element) -> str:
"""Get the inner HTML of an element as a string."""
if not element:
return ''
return ''.join(str(child) for child in element.children).strip()
def save_to_excel(data: dict, output_path: Path):
"""Save extracted data to Excel spreadsheet with multiple sheets."""
wb = openpyxl.Workbook()
wb.remove(wb.active)
# Director sheet
ws = wb.create_sheet(title='director')
ws.append(['image', 'name', 'name_url', 'role', 'bio', 'links_html'])
for item in data.get('director', []):
ws.append([item.get('image', ''), item.get('name', ''), item.get('name_url', ''),
item.get('role', ''), item.get('bio', ''), item.get('links_html', '')])
# Active members sheet
ws = wb.create_sheet(title='members')
ws.append(['image', 'name', 'name_url', 'role', 'bio', 'links_html'])
for item in data.get('members', []):
ws.append([item.get('image', ''), item.get('name', ''), item.get('name_url', ''),
item.get('role', ''), item.get('bio', ''), item.get('links_html', '')])
# Alumni postdocs sheet
ws = wb.create_sheet(title='alumni_postdocs')
ws.append(['name', 'name_url', 'years', 'current_position', 'current_position_url'])
for item in data.get('alumni_postdocs', []):
ws.append([item.get('name', ''), item.get('name_url', ''),
item.get('years', ''), item.get('current_position', ''),
item.get('current_position_url', '')])
# Alumni grads sheet
ws = wb.create_sheet(title='alumni_grads')
ws.append(['name', 'name_url', 'years', 'current_position', 'current_position_url'])
for item in data.get('alumni_grads', []):
ws.append([item.get('name', ''), item.get('name_url', ''),
item.get('years', ''), item.get('current_position', ''),
item.get('current_position_url', '')])
# Alumni managers sheet
ws = wb.create_sheet(title='alumni_managers')
ws.append(['name', 'name_url', 'years', 'current_position', 'current_position_url'])
for item in data.get('alumni_managers', []):
ws.append([item.get('name', ''), item.get('name_url', ''),
item.get('years', ''), item.get('current_position', ''),
item.get('current_position_url', '')])
# Alumni undergrads sheet
ws = wb.create_sheet(title='alumni_undergrads')
ws.append(['name', 'years'])
for item in data.get('alumni_undergrads', []):
ws.append([item.get('name', ''), item.get('years', '')])
# Collaborators sheet
ws = wb.create_sheet(title='collaborators')
ws.append(['name', 'url', 'description'])
for item in data.get('collaborators', []):
ws.append([item.get('name', ''), item.get('url', ''), item.get('description', '')])
# Adjust column widths
for sheet in wb.worksheets:
for col in range(1, 7):
sheet.column_dimensions[openpyxl.utils.get_column_letter(col)].width = 40
wb.save(output_path)
print(f"Saved to {output_path}")
def main():
project_root = Path(__file__).parent.parent
html_path = project_root / 'people.html'
output_path = project_root / 'data' / 'people.xlsx'
print(f"Extracting from: {html_path}")
data = extract_people(html_path)
save_to_excel(data, output_path)
print("Done!")
if __name__ == '__main__':
main()