Repository navigation
Expand file tree
/
Copy pathnewDoubleCountScraperHTML.py
More file actions
142 lines (116 loc) · 5.42 KB
/
Copy pathnewDoubleCountScraperHTML.py
File metadata and controls
142 lines (116 loc) · 5.42 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
from bs4 import BeautifulSoup as bs
import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
import csv
courses_sectors = pd.DataFrame(columns=['course_name', 'course_code', 'sector'])
courses_foundations = pd.DataFrame(columns=['course_name', 'course_code', 'foundations'])
options = webdriver.ChromeOptions()
options.add_argument('--headless')
# Make sure to install ChromeDriver!
browser = webdriver.Chrome(options=options, executable_path='/Users/bachtran/data/double-count/chromedriver')
# Change for current semester!
BASE_URL = "https://courses.upenn.edu/?srcdb=202410&course_attributes="
SECTORS = {
"I: Society" : "course_attributes_AUSO",
"II: History and Tradition" : "course_attributes_AUHT",
"III: Arts and Letters" : "course_attributes_AUAL",
"IV: Humanities and Social Science" : "course_attributes_AUHS",
"V: Living World" : "course_attributes_AULW",
"VI: Physical World" : "course_attributes_AUPW",
"VII: Natural Sciences Across Disciplines" : "course_attributes_AUNM",
}
FOUNDATIONS = {
"Quantitative Data Analysis" : "course_attributes_AUQD",
"Formal Reasoning & Analysis" : "course_attributes_AUFR",
"Cross Cultural Analysis" : "course_attributes_AUCC",
"Cultural Diversity in the U.S." : "course_attributes_AUCD"
}
def getData(url, req_name):
global courses_sectors
global courses_foundations
global browser
browser.get(url)
WebDriverWait(browser, 5).until(ec.presence_of_element_located((By.CLASS_NAME, 'result__headline')))
html = browser.page_source
soup = bs(html, "html.parser")
results = soup.find_all('span', class_='result__headline')
for result in results: # strip header row
code = result.find('span', class_='result__code').string
name = result.find('span', class_='result__title').string
if req_name in SECTORS.keys():
courses_sectors = courses_sectors.append({'course_name': name, 'course_code': code, 'sector': req_name}, ignore_index = True)
else:
courses_foundations = courses_foundations.append({'course_name': name, 'course_code': code, 'foundations': req_name}, ignore_index = True)
getData(BASE_URL + "course_attributes_AULG", "Language")
for name, querystr in SECTORS.items():
print("doing ", name)
getData(BASE_URL + querystr, name)
for name, querystr in FOUNDATIONS.items():
print("doing ", name)
getData(BASE_URL + querystr, name)
browser.quit()
def getFoundation(r):
name = r['course_name']
if not courses_foundations[courses_foundations['course_name'] == name].empty:
r['Foundation'] = courses_foundations[courses_foundations['course_name'] == name].iloc[0]['foundations']
return r
courses_sectors = courses_sectors.drop_duplicates()
courses_foundations = courses_foundations.drop_duplicates()
courses_combined = courses_sectors.apply(getFoundation, axis = 1)
courses_combined = courses_combined.dropna()
courses_combined = courses_combined.groupby('course_name').agg({'course_code': '<br>'.join, 'sector': 'first', 'Foundation': 'first'})
courses_combined = courses_combined.sort_values(['course_name']).reset_index()
courses_combined = courses_combined.rename(columns = {'course_name': 'Course Name', 'course_code': 'Course Code', 'sector': 'Sector'})
html_file_name = 'double_count_table_2024s.html'
with open(html_file_name, 'w+') as htmlfile:
courses_combined.to_html(htmlfile, escape = False)
courses_combined.to_csv('double_count2024s.csv')
## DON'T FORGET TO CHANGE TABLE STYLE AND INSERT SCRIPTS AT THE BOTTOM OF HTML FILE (see previous tables)!
# <table id="doublecount" class="table table-striped compact" style="width:100%;font-size:16px;">
# <colgroup>
# <col span="1" style="width: 2%;">
# <col span="1" style="width: 40%;">
# <col span="1" style="width: 18%;">
# </colgroup>
# <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
# <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
# <script type="text/javascript">
# $(document).ready(function() {
# $('#doublecount').DataTable({
# "order": [[ 0, "asc" ]]
# });
# } );
# </script>
# <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
# <style>
# [type="search"] {
# height: 28px;
# padding: 3px 12px;
# font-size: 14px;
# line-height: 1.428571429;
# color: #555;
# background-color: #fff;
# background-image: none;
# border: 1px solid #ccc;
# border-radius: 4px;
# }
# </style>
with open(html_file_name, 'r+') as htmlfile:
contents = htmlfile.read()
soup = bs(contents, 'lxml')
# print(soup.prettify())
soup.find('table')['id'] = 'doublecount'
soup.find('table')['class'] = 'table table-striped compact'
soup.find('table')['style'] = 'width:100%;font-size:16px;'
del soup.find('table')['border']
with open('double_count_footer.html', 'r') as htmlfooterfile:
footerContents = htmlfooterfile.read()
footerSoup = bs(footerContents, 'lxml')
for element in footerSoup.body:
print(element)
soup.append(element)
# htmlfile.write(soup.prettify())
htmlfile.write(str(soup.find('table')))