-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve.py
executable file
·133 lines (87 loc) · 3.64 KB
/
retrieve.py
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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import re
import time
def getHeadings(bodyPanel: WebElement) -> set:
h3_elements = bodyPanel.find_elements(By.TAG_NAME, "h3")
headings = set()
for h3 in h3_elements:
headings.add(h3.text)
return headings
def click_search(driver):
button = driver.find_element(By.XPATH,"/html/body/main/div[1]/div/div[2]/form/div[2]/div/div[5]/button")
button.click()
def wait_for_element(driver, locator):
WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))
def get_courses(driver) -> list:
panel_body = driver.find_element(By.XPATH,"/html/body/main/div[2]/div/div[3]")
courses = panel_body.find_elements(By.CSS_SELECTOR,".result.result--group-start")
print(len(courses))
return courses
def parse_course(course_ND: WebElement):
print(course_ND.text)
# class_ND.click()
options = Options()
# options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
driver.get("https://classsearch.nd.edu/")
print(f'scraping {driver.title}...')
titles = driver.find_element
click_search(driver)
# get list of courses
wait_for_element(driver,(By.CSS_SELECTOR,".result.result--group-start")) #waits for first class to appear
courses = get_courses(driver) # each is a webelement
#-------------for each course-------------------
# TEST
log = open("headings.txt","w")
head_code = None
courses[0].click()
wait_for_element(driver,(By.XPATH,"/html/body/main/div[3]/div/div[2]/div[1]")) #waits for first body block to appear
info_panel = driver.find_elements(By.CLASS_NAME,"panel__content")[2]
counter = 0
total_headings = set()
for c in courses:
isMany = int("sections" in c.text) # checks if class has multiple sections aka
if isMany: continue
course_code = c.find_element(By.CLASS_NAME, "result__code").text
c.click()
WebDriverWait(info_panel,10).until(EC.text_to_be_present_in_element((By.CLASS_NAME,"panel__head"), course_code))
body_panel = info_panel.find_element(By.CLASS_NAME,"panel__body")
counter+=1
print(counter)
# TEST - to see how what headings there in classsearch
current_headings = getHeadings(body_panel)
total_headings.update(current_headings)
print(f"{course_code} - {str(current_headings)}")
if counter == 500:
break
print(total_headings)
log.write(str(total_headings))
log.close()
exit()
courses[0].click()
wait_for_element(driver,(By.XPATH,"/html/body/main/div[3]/div/div[2]/div[1]")) #waits for first body block to appear
info_panel = driver.find_elements(By.CLASS_NAME,"panel__content")[2]
body_panel = info_panel.find_element(By.CLASS_NAME,"panel__body")
course_details = driver.find_element(By.XPATH,"/html/body/main/div[3]/div/div[2]")
components = course_details.find_elements(By.XPATH, './*')
for comp in components:
if comp.text == '': continue
print(comp.text)
print("-------------------------------")
def print_direct_children(root: WebElement, level=0):
children = root.find_elements(By.XPATH, "./*")
indent = "\t" * level
for child in children:
class_name = child.get_attribute('class')
if class_name == '':
print(f'{indent}{child.text}')
else:
print(f'{indent}{class_name}')
print_direct_children(child, level + 1)
print_direct_children(course_details)