-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_school_urls.rb
More file actions
149 lines (128 loc) · 4.35 KB
/
Copy pathget_school_urls.rb
File metadata and controls
149 lines (128 loc) · 4.35 KB
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
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'csv'
def accessingHTML(page)
Nokogiri::HTML(open(page))
end
#format school and province lists so they can be plugged into url (i.e. downcase and hyphonated)
def formatLists(array, option)
if option.text.include? " "
formatted_option = option.text.downcase.gsub!(/\s+/, '-')
else
formatted_option = option.text.downcase
end
array << formatted_option
end
#get school and province lists
def getLists(url, provinces, school_type)
page = accessingHTML(url)
#get school type list
page.css("body div.tab-content div#school-location-search select#categories option").each do |option|
formatLists(school_type, option)
end
#get provinces list
page.css("body div.tab-content div#school-location-search select#jr_province option").each do |option|
formatLists(provinces, option)
end
end
#get the total number of pages from the text at the top of the page that lists total results
def getNumPages(home_page)
num_results = nil
get_results_string = home_page.css("section div div div div.jrCol4.jrPagenavResults").text.gsub(/^ ([0-9])+/)
#get result out of nokagiri enumerator into a variable
get_results_string.each do |extracted_string|
num_results = extracted_string.split.first.to_i
end
#return total number of pages
return num_pages = (num_results.to_f/10).ceil
end
def getProvincePages(url_first, url_second, array)
page_number = 1
page = accessingHTML(url_first + "#{page_number}" + url_second)
num_pages = getNumPages(page)
while page_number <= num_pages
page_url = url_first + "#{page_number}" + url_second
array << page_url
puts "stored province page #{page_number}"
page_number += 1
end
end
def getPrivateSchoolPages(url_first, url_second, array, provinces)
provinces.each do |province|
page_num = 1
page = accessingHTML(url_first + "#{page_num}" + url_second + province)
puts "---------------------------#{province.upcase}-------------------------------"
puts url_first + "#{page_num}" + url_second + province
num_pages = getNumPages(page)
while page_num <= num_pages
page_url = url_first + "#{page_num}" + url_second + province
puts "storred private school #{province} page #{page_num}"
array << page_url
page_num += 1
end
end
end
def getSchoolLinks(array_of_pages, array)
link_count=1
array_of_pages.each do |url|
page = accessingHTML(url)
links_element = page.css('tr td div div a')
links_element.each do |element|
link = "https://www.schoolguide.co.za/" + element['href']
if link.end_with? ".html"
array << link
puts "got link no. #{link_count}"
link_count+=1
end
end
end
end
#instantiate list vars and get lists
provinces = []
school_type = []
home_url = "https://www.schoolguide.co.za/"
getLists(home_url, provinces, school_type)
provinces.shift
school_type.shift
#instantiate Western Cape array and get pages
westernCapePages = []
url_first = "https://www.schoolguide.co.za/all-in/search-results.html?page="
url_second = "&order=featured&query=all&jr_province=western-cape"
getProvincePages(url_first, url_second, westernCapePages)
#instantiate Gauteng array and get pages
gautengPages = []
url_first = "https://www.schoolguide.co.za/all-in/search-results.html?page="
url_second = "&order=featured&query=all&jr_province=gauteng"
getProvincePages(url_first, url_second, gautengPages)
#instantiate Private School array and get pages
privatSchoolPages = []
url_first = "https://www.schoolguide.co.za/schools/private-schools/search-results.html?page="
url_second = "&order=featured&query=all&jr_province="
getPrivateSchoolPages(url_first, url_second, privatSchoolPages, provinces)
westernCapeLinks = []
privateSchoolLinks = []
gautengSchoolLinks = []
link_count = 1
#Get links from pages
getSchoolLinks(westernCapePages, westernCapeLinks)
getSchoolLinks(privatSchoolPages, privateSchoolLinks)
getSchoolLinks(gautengPages, gautengSchoolLinks)
#write Western Cape links to CSV
CSV.open("westernCapeLinks.csv", "w") do |csv|
puts "writing Western Cape pages to CSV..."
csv << westernCapeLinks
puts "success"
end
#write Gauteng links to CSV
CSV.open("gautengPages.csv", "w") do |csv|
puts "writing Gauteng pages to CSV..."
csv << gautengSchoolLinks
puts "success"
end
#write private school links to CSV
CSV.open("privateSchoolLinks.csv", "w") do |csv|
puts "writing private school pages to CSV..."
csv << privateSchoolLinks
puts "success"
end