Skip to content

Instantly share code, notes, and snippets.

@Noble-Mushtak
Created January 16, 2021 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noble-Mushtak/e9f9dd469f95535f4c0ba7066c4ec70c to your computer and use it in GitHub Desktop.
Save Noble-Mushtak/e9f9dd469f95535f4c0ba7066c4ec70c to your computer and use it in GitHub Desktop.
Python script to find out who has taught previous Northeastern courses.
"""
Script to find out who has taught previous Northeastern courses.
Prerequisites:
- Have Python bindings for Selenium installed (https://selenium-python.readthedocs.io/)
- Have chromedriver installed and in PATH environment variable (http://chromedriver.storage.googleapis.com/index.html)
Usage: python3 check_course.py `subject_name` `subject_code` `course_number`
Examples:
python3 check_course.py Mathematics MATH 4541
python3 check_course.py "Computer Science" CS 4400
"""
import sys
import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
subject_name = sys.argv[1]
subject_code = sys.argv[2]
course_number = sys.argv[3]
start_year = 2016
end_year = 2021
# Add all semesters from Fall start_year-1 to Spring end_year,
# from most recent to least recent
semesters = []
for year in range(end_year, start_year-1, -1):
semesters.append((f"Spring { year }", str(year)+"30"))
semesters.append((f"Fall { year-1 }", str(year)+"10"))
def no_op():
"""
Do nothing
"""
pass
def look_for_element_by_id(driver, id, setup=no_op):
"""
Return driver.find_element_by_id(id), once it exists
"""
while True:
try:
elem = driver.find_element_by_id(id)
return elem
except NoSuchElementException:
setup()
driver = webdriver.Chrome()
for semester_name, semester_id in semesters:
print(f"Begin search for { semester_name }")
# Go to course search page
driver.get("https://nubanner.neu.edu/StudentRegistrationSsb/ssb/term/termSelection?mode=search")
def setup_semester_search():
"""
Searches for semester in the "Select a Term" page
We have to use this setup function, because sometimes,
we need to call this setup multiple times for it to work:
If the initial setup is too fast, then it will complete before the page completely loads
and then will be overwritten by our last search result
because the last search result is the default value.
"""
# Several errors are possible here,
# so we'll just keep trying until there are no errors.
# (Not the highest quality code, I know.)
try:
# Click on the semester search box
semester_search_box = driver.find_element_by_id("s2id_txt_term")
semester_search_box = semester_search_box.find_element_by_class_name("select2-choice")
semester_search_box.click()
# Enter the semester name
semester_search_input = driver.find_element_by_id("s2id_autogen1_search")
semester_search_input.clear()
semester_search_input.send_keys(semester_name+" Semester")
except:
pass
# Keep setting up the search result until the search result pops up
semester_search_result = look_for_element_by_id(
driver, semester_id, setup_semester_search
)
semester_search_result.click()
# Click the Continue button
semester_search_submit = driver.find_element_by_id("term-go")
semester_search_submit.click()
# Keep trying to click on the subject search box
# until the page finishes loading
subject_search_box = look_for_element_by_id(driver, "s2id_txt_subject")
subject_search_box.click()
# Enter the subject name
subject_search_input = driver.find_element_by_id("s2id_autogen1")
subject_search_input.send_keys(subject_name)
# Wait for search result to load before selecting result
subject_search_result = look_for_element_by_id(driver, subject_code)
subject_search_result.click()
# Enter the course number
course_search_input = driver.find_element_by_id("txt_courseNumber")
course_search_input.send_keys(course_number)
# Click the Search button
course_search_submit = driver.find_element_by_id("search-go")
course_search_submit.click()
# Wait for the page to load
table = look_for_element_by_id(driver, "table1")
# Get the instructors of each course
instructor_elems = driver.find_elements_by_css_selector("[data-property=instructor]")
# If there are no instructors, output indication message.
# Notice that we check < 2 because the first element in instructor_elems
# will just be the "Instructor" heading in the table.
if len(instructor_elems) < 2:
print("No sections of this course found")
else:
for instructor_elem in instructor_elems[1:]:
# Separate different instructors by semicolons
instructors_lst = "; ".join(instructor_elem.text.split("\n"))
print(f"Section taught by { instructors_lst }")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment