-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudget_webScrape_beta.py
217 lines (177 loc) · 6.71 KB
/
budget_webScrape_beta.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import requests
from bs4 import BeautifulSoup
""" for use with the NYC DOE Galaxy budget page at:
https://www.nycenet.edu/offices/d_chanc_oper/budget/dbor/galaxy/galaxybudgetsummaryto/display2.asp?DDBSSS_INPUT=K662&Submit=Enter&PROG_YEAR=2017&POP_SCH=K662
http://schools.nyc.gov/Common/Templates/MainTemplate/CommonMainTemplate.aspx?NRMODE=Published&NRNODEGUID=%7b9E62DB6A-8687-40E3-93D0-5FF6FA11C46D%7d&NRORIGINALURL=%2fAboutUs%2ffunding%2fschoolbudgets%2fGalaxyAllocationFY2017%2ehtm%3fBSSS_INPUT%3d04K211&NRCACHEHINT=Guest&BSSS_INPUT=04K211
TO-DO:
3. School Name
5. Add in better specification
6. Make quicker
NOTE: Functions copy a lot of code in case formatting differs year to year.
Time spent: 4.5 hours
Made by James Gan for Practice Makes Perfect
Available under The MIT License:
Copyright 2017 James Gan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
codes = []
keywords = []
with open ('schools.txt', 'rb') as fp:
content = fp.readlines()
for line in content:
line = str(line.strip())[2:8]
codes.append(line)
with open ('keywords.txt', 'rb') as fp:
content = fp.readlines()
for line in content:
line = str(line.strip())[2:8]
keywords.append(line)
def budgetFetch(ATS, year):
# figure out URL format
url = "https://www.nycenet.edu/offices/d_chanc_oper/budget/dbor/galaxy/"+\
"galaxybudgetsummaryto/display2.asp?DDBSSS_INPUT="+ATS[2:]+\
"&Submit=Enter&PROG_YEAR="+year+"&POP_SCH="+ATS[2:]
page = requests.get(url)
if(page.status_code!=200):
return "ERROR"
soup = BeautifulSoup(page.content, 'html.parser')
lines = []
# Better looking output: nothing colliding
for line in soup.find_all('tr', class_='mysection'):
lineStr = line.get_text()
if len(lineStr) > 0:
for i in range(0, len(lineStr)-1):
if not lineStr[i].isupper() and lineStr[i+1].isupper() and \
lineStr[i].isalpha() and lineStr[i+1].isalpha():
lineStr = lineStr[:i+1] + ' ' + lineStr[i+1:]
# lame hardcode
lineStr = lineStr.replace("0.00", "")
lineStr = lineStr.replace("1.00", "")
lineStr = lineStr.replace("2.00", "")
lineStr = lineStr.replace("3.00", "")
lineStr = lineStr.replace("4.00", "")
lineStr = lineStr.replace("5.00", "")
lineStr = lineStr.replace("6.00", "")
lineStr = lineStr.replace("7.00", "")
lineStr = lineStr.replace("8.00", "")
lineStr = lineStr.replace("9.00", "")
lineStr = lineStr.replace("0.50", "")
lineStr = lineStr.replace("OTPS", "")
lineStr = lineStr.replace("SBST", "")
lines.append(lineStr)
# O(n^2+n), could improve
for line in lines:
printed = False
for term in keywords:
if (((term) in line) and not printed):
printed = True
#print(line)
with open("Budgets.txt", "a") as text_file:
text_file.write(line+'\n')
linesTotal = []
printedP = False
# Finding total and principal
# Better looking output: nothing colliding
# O(n^2+n), could improve
for line in soup.find_all('tr', class_='myschool'):
lineStr = line.get_text()
if len(lineStr) > 0:
for i in range(0, len(lineStr)-1):
if not lineStr[i].isupper() and lineStr[i+1].isupper() and \
lineStr[i].isalpha() and lineStr[i+1].isalpha():
lineStr = lineStr[:i+1] + ' ' + lineStr[i+1:]
# lame hardcode
lineStr = lineStr.replace("OTPS", "")
lineStr = lineStr.replace("SBST", "")
lineStr = lineStr.replace(ATS, "")
# add total
if '$' in lineStr:
lineStr = lineStr.replace("$", " $")
lineStr = "Positions & Total Budget: $ "+lineStr
linesTotal.append(lineStr)
# add principal
if printedP is False:
if ((", " in lineStr) and not lineStr[lineStr.find(',')-1].isupper()):
lineStr = "Principal: $ "+lineStr
linesTotal.append(lineStr.strip())
printedP = True
for line in linesTotal:
with open("Budgets.txt", "a") as text_file:
text_file.write(line+'\n')
def FSF(ATS, year):
# figure out URL format
url = "http://schools.nyc.gov/AboutUs/funding/schoolbudgets/GalaxyAllocationFY"+\
year+".htm?BSSS_INPUT="+ATS[2:]
page = requests.get(url)
if(page.status_code!=200):
return "ERROR"
soup = BeautifulSoup(page.content, 'html.parser')
lines = []
seen = False
for line in soup.find_all('tr'):
lineStr = line.get_text()
# find FSF funding
if "TL Fair Student Funding" in lineStr:
if seen == False:
seen = True
elif seen == True:
lineStr = lineStr.replace(" ", " $ ")
lines.append(lineStr)
# O(n^2+n), could improve
for term in keywords:
seenterm = False
if term in lineStr:
if seenTerm == False:
seenTerm = True
elif seenTerm == True:
lineStr = lineStr.replace(" ", " $ ")
# Better looking output: nothing colliding
if len(lineStr) > 0:
for i in range(0, len(lineStr)-1):
if not lineStr[i].isupper() and lineStr[i+1].isupper() and \
lineStr[i].isalpha() and lineStr[i+1].isalpha():
lineStr = lineStr[:i+1] + ' ' + lineStr[i+1:]
lines.append(lineStr)
for line in lines:
printedF = False
with open("Budgets.txt", "a") as text_file:
text_file.write(line+'\n')
def main():
print("Running...")
for code in codes:
print("Running for: "+code)
with open("Budgets.txt", "a") as text_file:
text_file.write('\n\n'+code)
# Paste below here to add a new year
# Copy from here to add a new year
with open("Budgets.txt", "a") as text_file:
text_file.write('\n'+"2017"+'\n\n')
budgetFetch(code,"2017")
FSF(code,"2017")
# Copy to here to add a new year
with open("Budgets.txt", "a") as text_file:
text_file.write('\n'+"2016"+'\n\n')
budgetFetch(code,"2016")
FSF(code,"2016")
with open("Budgets.txt", "a") as text_file:
text_file.write('\n'+"2015"+'\n\n')
budgetFetch(code,"2015")
FSF(code,"2015")
with open("Budgets.txt", "a") as text_file:
text_file.write('\n'+"2014"+'\n\n')
budgetFetch(code,"2014")
FSF(code,"2014")
print("Done!")