Created
July 18, 2018 18:05
-
-
Save AO8/eb7d845794fc9a9fd80b429db598a53e to your computer and use it in GitHub Desktop.
A slightly different version of previous IT enrollment Python web scraper project. Includes GUI automation with pyautogui, scraping with BeautifulSoup, and sending gmail notifications with smtplib and MIMEText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Standard Library | |
| import smtplib | |
| import webbrowser | |
| from time import sleep | |
| from datetime import datetime as dt | |
| from urllib.request import urlopen | |
| from email.mime.text import MIMEText | |
| # Third-party | |
| import pyautogui | |
| from bs4 import BeautifulSoup | |
| # Function for sending email | |
| def send_gmail(from_addr, password, to_addr, subject, body): | |
| msg = MIMEText(body) | |
| msg["Subject"] = subject | |
| msg["From"] = from_addr | |
| msg["To"] = to_addr | |
| s = smtplib.SMTP("smtp.gmail.com: 587") | |
| s.ehlo() | |
| s.starttls() | |
| s.login(from_addr, password) | |
| s.send_message(msg) | |
| s.quit() | |
| # Navigate to IT web page, no direct URL available hence need for GUI automation | |
| webbrowser.open("https://www.greenriver.edu/grc/classfinder/") | |
| sleep(5) | |
| # GUI automation to select IT from drop-down menu | |
| pyautogui.click(1159, 390) # coordinates specific to local machine | |
| sleep(1) | |
| pyautogui.typewrite("IT") | |
| pyautogui.press("enter") | |
| sleep(1) | |
| pyautogui.click(1656, 458) # coordinates specific to local machine | |
| sleep(3) | |
| # GUI automation to save IT enrollment page as local html file | |
| pyautogui.click(button="right") # right click to save | |
| sleep(5) | |
| pyautogui.click(1641, 604) # save as, coordinates specific to local machine | |
| sleep(5) | |
| pyautogui.click(709, 667) | |
| sleep(5) | |
| pyautogui.click(1044, 536) # replace existing copy, coordinates specific to local machine | |
| sleep(5) | |
| # Create a BeautifulSoup object from local html file | |
| html_url = r"file:///C:/Users/employee/Desktop/Class%20Finder%20_%20Green%20River%20College.html" | |
| html = urlopen(html_url) | |
| soup = BeautifulSoup(html.read(), "html.parser") | |
| # Scrape IT enrollment information | |
| enrollments = [] | |
| for table in soup.find_all("table"): | |
| course = table.attrs["data-courseid"] | |
| for td in table.find_all("td", {"data-th":"Status"}): | |
| status = td.get_text() | |
| enrollments.append(course + ": " + status) | |
| # Calculate totals for email | |
| bas_nas_courses = ["310", "335", "340", "344", "360", "370", "385", "390", "410", "460", "490"] | |
| bas_sd_courses = ["301", "305", "328", "333", "334", "355", "372", "378", "426", "485", "486"] | |
| bas_nas = 0 | |
| bas_sd = 0 | |
| aas = 0 | |
| waitlist = 0 | |
| for i in enrollments: | |
| if i[10:14] == "WAIT": | |
| space1 = i.find(" ") | |
| space2 = i.find(" ", space1+1) | |
| space3 = i.find(" ", space2+1) | |
| space4 = i.find(" ", space3+1) | |
| space5 = i.find(" ", space4+1) | |
| space6 = i.find(" ", space5+1) | |
| space7 = i.find(" ", space6+1) | |
| space8 = i.find(" ", space7+1) | |
| space9 = i.find(" ", space8+1) | |
| waitlist += int(i[space8+1:space9]) | |
| if i[10:16] != "Cancel": | |
| space1 = i.find(" ") | |
| space2 = i.find(" ", space1+1) | |
| space3 = i.find(" ", space2+1) | |
| space4 = i.find(" ", space3+1) | |
| space5 = i.find(" ", space4+1) | |
| space6 = i.find(" ", space5+1) | |
| reg = int(i[space5+1:space6]) | |
| if i[5:8] in bas_nas_courses: | |
| bas_nas += reg | |
| elif i[5:8] in bas_sd_courses: | |
| bas_sd += reg | |
| else: | |
| aas += reg | |
| # Prepare email of IT enrollments to stakeholders | |
| report_time = dt.now().strftime("%I:%M %p on %x") | |
| stakeholders = ["#",] # enter To: email addresses, as many as needed | |
| body = "B892 enrollments as of " + report_time + ".\n\n\ | |
| AAS: " + str(aas) + "\n\ | |
| BAS-NAS: " + str(bas_nas) + "\n\ | |
| BAS-SD: " + str(bas_sd) + "\n\ | |
| Total: " + str(bas_nas + bas_sd + aas) + "\n\ | |
| Total waitlisted: " + str(waitlist) + "\n\n" | |
| for enrollment in enrollments: | |
| body += (enrollment + "\n") | |
| body += "\nThis Python bot was built with love by AndyO." | |
| # Send email to stakeholders | |
| for address in stakeholders: | |
| send_gmail("#from gmail address", "#password", address, "Fall IT Enrollments", body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment