-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrappingVSFS01.py
299 lines (227 loc) · 7.67 KB
/
scrappingVSFS01.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import requests
import time
import sys
import webbrowser
from bs4 import BeautifulSoup as Soup
globalRequest = {
'agenda' : 'th',
'FAK' : '6410',
'PRI' : '-',
'ROK' : '2017',
'TIT' : 'Bc.',
'PRA' : '-',
#'search' : 'Vyhledat'
'vypsat' : '1',
'exppar' : '1',
'por' : '51'
}
URL_WITH_ADDITIONAL_INFO = "https://is.vsfs.cz"
def extractSearchingParam(soupPage):
arrayOfParam = []
dOrigin = {'exppar':'1', 'por':'1'}
array = soupPage.find("div", {"style":"margin: 9px;"}).find_all('a')
if ( array[0].get('href') == '#' ):
arrayOfParam.append(dOrigin)
return arrayOfParam
for param in array:
paramArray = param.get('href').split(';')
por = paramArray[-1].split('=')[-1]
exppar = paramArray[-2].split('=')[-1]
dCopy = dOrigin.copy()
dCopy['por'] = por
dCopy['exppar'] = exppar
arrayOfParam.append(dCopy)
return arrayOfParam
def processThesis(thesis, fileObject):
global URL_WITH_ADDITIONAL_INFO, globalRequest
thesisInfo = thesis.find_all("i")
if (len(thesisInfo) > 7):
possibleName = thesisInfo[0]
nameConvert = ''
for letter in possibleName:
nameConvert += str(letter)
if ( "roz." in nameConvert ):
print("Roz. problem")
del thesisInfo[0]
if ( len(thesisInfo) != 7 ):
print("Bad number of data len: ", len(thesisInfo))
year = extractYearFromList(thesisInfo)
successRate = extractSuccessRateFromList(thesisInfo)
extraLink = extractLink(thesis)
#print("url: " + URL_WITH_ADDITIONAL_INFO + extraLink)
requestForPage = handleRequestGET(URL_WITH_ADDITIONAL_INFO + extraLink, 3)
if requestForPage.status_code != 200:
print("Cannot handle request with status code: ", requestForPage.status_code)
time.sleep(10)
fileObject.close()
return
soupWebPage = Soup(requestForPage.content, "html5lib")
authorName = soupWebPage.h3.b.string
nameOfThesis = soupWebPage.find_all('h2')[1].string
subjectOfStudy = soupWebPage.find('div', {'class':'oddil'}).em.string.split('/')[1]
typeOfThesis = globalRequest['TIT']
nameOfSupervisor = extractSupervisor(soupWebPage)
nameOfOponent = extractOponent(soupWebPage)
authorName = authorName.replace(',', ' ')
nameOfThesis = nameOfThesis.replace(',', ' ')
subjectOfStudy = subjectOfStudy.replace(',', ' ')
nameOfSupervisor = nameOfSupervisor.replace(',', ' ')
nameOfOponent = nameOfOponent.replace(',', ' ')
'''
print("author: " + authorName)
print("year: " + year)
print("type of thesis:" + typeOfThesis)
print("subjectOfStudy: " + subjectOfStudy)
print("nameOfThesis: " + nameOfThesis)
print("succes: " + successRate)
print("supervisor: " + nameOfSupervisor)
print("oponent: " + nameOfOponent)
'''
csvRow = authorName+','+nameOfThesis+','+subjectOfStudy+','+typeOfThesis+','+year+','+successRate+','+nameOfSupervisor+','+nameOfOponent+'\n'
'''
print("author: "+authorName+"; year: "+year+"; supervisor: "+nameOfSupervisor+"; oponent: "+nameOfOponent)
print("nameOfThesis: "+nameOfThesis+"; subjectOfStudy: "+subjectOfStudy)
print("Succes: "+successRate)
'''
if len(csvRow.split(',')) == 8:
fileObject.write(csvRow)
print("Succesful write to CSV file")
else:
print("Bad CSV format!!!")
def checkRequest(requestForPage):
if requestForPage.status_code != 200:
captchaPageSoup = Soup(requestForPage.content, "html5lib")
captchaPage = captchaPageSoup.find_all('a')[5].get('href')
print("Go to the folowing page and resolve a captcha code:")
print(URL_WITH_ADDITIONAL_INFO + captchaPage)
print()
webbrowser.open(URL_WITH_ADDITIONAL_INFO + captchaPage)
time.sleep(5)
print("remain 10 seconds...")
time.sleep(10)
return False
return True
def handleRequestGET(urlString, nAttempts):
for i in range(nAttempts):
requestForPage = requests.get(urlString)
isRequestOK = checkRequest(requestForPage)
if (isRequestOK == True):
break
return requestForPage
def handleRequestPOST(urlString, myRequest, nAttempts):
for i in range(nAttempts):
requestForPage = requests.post(urlString, myRequest)
isRequestOK = checkRequest(requestForPage)
if (isRequestOK == True):
break
return requestForPage
def extractName(thesis):
rawName = thesis.find("b").string
rawName = rawName.replace(',', '')
return rawName
def extractYearFromList(thesisInfo):
year = thesisInfo[1].string
return year
def extractSubjectOfStudyFromList(thesisInfo):
subjectOfStudy = thesisInfo[5].string
return subjectOfStudy
def extractSuccessRateFromList(thesisInfo):
success = thesisInfo[2].string
succesList = success.split(" ")
if "úspěšně" in succesList:
return "1"
elif "neúspěšně" in succesList:
return "0"
else:
return "-1"
def extractNameOfThesisFromList(thesisInfo):
nameOfThesis = thesisInfo[6].string
return nameOfThesis
def extractLink(thesis):
extralink = thesis.find("a")["href"]
return extralink
def extractSupervisor(htmlPage):
infoAbouSupervisor = htmlPage.find_all('ul')[4].find('li').string.split(', ')
rawName = infoAbouSupervisor[0]
return rawName
def extractOponent(htmlPage):
infoAbouSupervisor = htmlPage.find_all('ul')[5].find('li').string.split(', ')
rawName = infoAbouSupervisor[0]
return rawName
def initRequestParam(additionalParam):
global globalRequest
globalRequest['exppar'] = additionalParam['exppar']
globalRequest['por'] = additionalParam['por']
def extractListOfThesis(soupPage):
listOfThesis = soupPage.find_all(style="margin-left:50px")
return listOfThesis
def processCollectionOfThesis(listOfThesis, fileObject):
n = 0
for thesis in listOfThesis:
print(n)
print("-----------------------------------------")
processThesis(thesis, fileObject)
print("-----------------------------------------")
print()
n += 1
def openFile(filename):
fileExists = True
csvHead = 'Autor,Název práce,Studijní program,Typ práce,Rok,Obhájeno,Vedoucí,Oponent\n'
try:
tryFile = open(filename, 'r')
except:
fileExists = False
else:
tryFile.close()
if (fileExists == False):
csvFile = open(filename, 'a')
csvFile.write(csvHead)
else:
csvFile = open(filename, 'a')
return csvFile
def processSpecificTypeOfThesis(year, typeThesis):
global URL_WITH_ADDITIONAL_INFO, globalRequest
globalRequest['ROK'] = year
globalRequest['TIT'] = typeThesis
globalRequest['exppar'] = 1
globalRequest['por'] = 1
globalRequest['vypsat'] = 1
baseURL = 'https://is.vsfs.cz/thesis/'
csvFile = openFile(globalRequest['TIT']+globalRequest['ROK']+'.csv')
requestForPage = handleRequestPOST(baseURL, globalRequest, 3)
if requestForPage.status_code != 200:
print("Cannot handle request with status code: ", requestForPage.status_code)
time.sleep(10)
return
soupPage = Soup(requestForPage.text, "html5lib")
listOfParam = extractSearchingParam(soupPage)
for param in listOfParam:
print("==================================")
print('por: ', param['por'])
print("==================================")
initRequestParam(param)
requestForPage = handleRequestPOST(baseURL, globalRequest, 2)
if requestForPage.status_code != 200:
print("Cannot handle request with status code: ", requestForPage.status_code)
time.sleep(10)
return
soupPage = Soup(requestForPage.text, "html5lib")
listOfThesis = extractListOfThesis(soupPage)
processCollectionOfThesis(listOfThesis, csvFile)
csvFile.close()
#========================================================
# MAIN
#========================================================
# example of usage from terminal
# -scrappingVSFS001.py 2017 Bc.
# -scrappingVSFS001.py 2016 Bc.
# -scrappingVSFS001.py 2012 Ing.
# mergin in bash:
# head -q -n 1 Bc.2009.csv > allThesis.csv
# tail -q -n +2 *.*.csv >> allThesis.csv
year = sys.argv[1]
typeOfThesis = sys.argv[2]
processSpecificTypeOfThesis(year, typeOfThesis)
#========================================================
# MAIN
#========================================================