Skip to content

Instantly share code, notes, and snippets.

@colebob9
Last active October 15, 2017 19:07
Show Gist options
  • Save colebob9/3a3463098fe014eb0a78722ade0b6c6e to your computer and use it in GitHub Desktop.
Save colebob9/3a3463098fe014eb0a78722ade0b6c6e to your computer and use it in GitHub Desktop.
Scrapes CodeHS student profiles for statistics
# CodeHSScrape v1.1
# Python 3
import time
import csv
from selenium import webdriver
browser = webdriver.Firefox(executable_path='./geckodriver')
browser.get('https://codehs.com/login')
# username / pass
emailElem = browser.find_element_by_id('login-email')
emailElem.send_keys('botteacher@example.com')
passwordElem = browser.find_element_by_id('login-password')
passwordElem.send_keys('password')
passwordElem.submit()
time.sleep(1)
browser.get('CodeHS class link here')
elems = browser.find_elements_by_xpath("//*[@id=\"student-action-icons\"]/a[1]")
profiles = []
for elem in elems:
profiles.append(elem.get_attribute("href"))
print(profiles)
datetime = time.strftime("%m-%d-%Y--%I:%M%p")
outputFile = open('stats.csv', 'w', newline='')
outputWriter = csv.writer(outputFile)
outputWriter.writerow(["Time:", datetime])
outputWriter.writerow(["Name:", "Course Progress:", "Points:", "Days Coding:", "Badges:", "Coding Streak:"])
outputWriter.writerow([''])
# open up each profile
for link in profiles:
browser.get(link)
# Get Profile Stats
name = browser.find_element_by_xpath("//*[@id=\"user-page-sidenav\"]/p").text
courseProgress = browser.find_element_by_xpath("//*[@id=\"recent-module\"]/a/p[2]").text
points = browser.find_element_by_xpath("//*[@id=\"user-stats\"]/tbody/tr/td[2]/p[2]").text
daysCoding = browser.find_element_by_xpath("//*[@id=\"user-stats\"]/tbody/tr/td[3]/p[2]").text
badges = browser.find_element_by_xpath("//*[@id=\"user-stats\"]/tbody/tr/td[4]/p[2]").text
codingStreak = browser.find_element_by_xpath("//*[@id=\"user-stats\"]/tbody/tr/td[5]/p[2]").text
# Putting data into a csv file
print([name, courseProgress, points, daysCoding, badges, codingStreak])
outputWriter.writerow([name, courseProgress, points, daysCoding, badges, codingStreak])
outputFile.close()
browser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment