forked from wboag/SPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.rkt
187 lines (150 loc) · 7.88 KB
/
scraper.rkt
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CourseGrid:scraper.rkt ;;
;; ;;
;; Purpose: Crawl MIT OCW to gather data about available courses. ;;
;; ;;
;; Author: Willie Boag [email protected] ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket
(require (planet neil/html-parsing:2:0))
(require "url-lib.rkt")
(require "html-lib.rkt")
(require "course.rkt")
(provide department->courses)
(provide course->prereqs-text)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Scrape department page for course listings ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Extract course name, number, level, and URL from html block
; ex (html->course <string-of-html>) -> <course-obj>
(define (html->course ht-data)
(let
((course-name (eliminate-duplicate-whitespace
(shallow-get-text
(get-attr (cadr (get-attr-all ht-data 'td))
'a))))
(course-number (eliminate-duplicate-whitespace
(shallow-get-text (get-attr-path ht-data '(td a)))))
(course-url (resolve-ocw-url
(get-field (get-attr (caddr (get-attr-all ht-data 'td)) 'a)
'href)))
(undergrad? (eliminate-duplicate-whitespace
(shallow-get-text (get-attr (caddr (get-attr-all ht-data 'td))
'a)))))
(make-course course-name course-number course-url undergrad?)))
; convert relative URL from website to absolute URL
(define (resolve-ocw-url url)
(string-append "http://ocw.mit.edu" url "/"))
; department name string to list of course objects
; ex. (department->courses "mathematics") --> '(<course-A> <course-B> ... <course-N>)
(define (department->courses department)
(let ((html-nested-toks (html->xexp ; read url
(url->port
(string-append "http://ocw.mit.edu/courses/"
department)))))
;(displayln (string-append "http://ocw.mit.edu/courses/"
; department))
;(displayln html-nested-toks)
(let ((html-for-courses
(get-attr-path html-nested-toks
'(html body (div (@ (id "center_global")))
(div (@ (id "grid")))
(div (@ (id "left")))
(div (@ (class "global_wrapper")))
(div (@ (id "global_inner")))
(table (@ (class "courseList")))
tbody ))))
; error?
(if (equal? html-for-courses 'BAD-QUERY)
; propogate error to top
'BAD-QUERY
; get data from each course
(map html->course
; go to section of html containing course data
; argument to get-attr-all is "BAD-QUERY"
(get-attr-all html-for-courses 'tr))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Scrape course home page to find syllabus ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Get the syllabus for a given course
; (get-syllabus <course-obj>) --> <string-url-to-syllabus>
(define (get-syllabus c)
(let*
((url (course-url c))
(parsed (html->xexp (url->port url)))
(path (get-attr-path parsed '(html body (div (@ (id "center_chp" )))
(div (@ (id "grid" )))
(div (@ (id "left" )))
(div (@ (id "course_wrapper")))
(div (@ (id "course_nav" )))
ul
(li (@ (class "" )))
a))))
; BAD FORMAT - http://ocw.mit.edu/courses/mathematics/18-03sc-differential-equations-fall-2011/
;(displayln url)
;(displayln (get-attr-path parsed '(html body (div (@ (id "center_chp" )))
; (div (@ (id "grid" )))
; (div (@ (id "left" )))
; (div (@ (id "course_wrapper")))
; (div (@ (id "course_nav" )))
; ul
; (li (@ (class "" ))))))
;(displayln path)
(if (equal? path 'BAD-QUERY)
'BAD-QUERY
(resolve-ocw-url (get-field path 'href)))))
; Get the written prereq text for a given course
; ex. (course->prereqs-text <course-obj>) --> <string-of-prereq-text>
(define (course->prereqs-text c)
(let*
((url (get-syllabus c)))
(if (equal? url 'BAD-QUERY)
'COULD-NOT-FIND-SYLLABUS
(let*
((parsed (html->xexp (url->port url)))
(path (get-attr-path parsed '(html body (div (@ (id "center" )))
(div (@ (id "grid" )))
(div (@ (id "left-section" )))
(div (@ (id "course_wrapper_section")))
(div (@ (id "course_inner_section" )))
))))
(define (printer t)
(cond
((pair? t) (eliminate-duplicate-whitespace (deep-get-text t)))
((string? t) (eliminate-duplicate-whitespace t))
(else t)))
(define (is-non-whitespace? s)
(if (string? s)
(not (equal? "" (eliminate-duplicate-whitespace s)))
#f))
(let
((strings (filter is-non-whitespace? (map printer path))))
;(displayln strings)
; NOTE: cadr assumption assumes all in one block
(cond
((member "Prerequisites" strings)
(cadr (member "Prerequisites" strings)))
((member "Prerequisite" strings)
(cadr (member "Prerequisite" strings)))
(else
'COULD-NOT-FIND-PREREQUISITES)
))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Test tools. ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|
(define (psuedo-unit-tests)
; URL port
(define html-data (url->port (resolve-ocw-url "/courses/mathematics/")))
; List of string tokens
(define parsed (html->xexp html-data))
; HTML table (AKA string of tokens) containing (among other thnings) course data
(define course-html-list (get-attr-path parsed '(html body (div (@ (id "center_global"))) (div (@ (id "grid"))) (div (@ (id "left"))) (div (@ (class "global_wrapper"))) (div (@ (id "global_inner"))) (table (@ (class "courseList"))) tbody )))
; List of (list of string HTML tokens for class preview)
(define courses (get-attr-all course-html-list 'tr))
; One list of string HTML tokens for class preview
(define course (car courses))
; Course object
(define course-data (html->course course))
)
|#