-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (196 loc) · 8.69 KB
/
main.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
import scrapy
import csv
from scrapy.crawler import CrawlerProcess
import configparser
class SpidyResults(scrapy.Spider):
name = 'results'
config = configparser.RawConfigParser()
config.read('settings.cfg')
settings = config['SETTINGS']
form_dict = {}
for k, v in settings.items():
form_dict[k] = v
form_url = settings['FormUrl']
start_urls = [settings['StudentNameUrl']]
download_delay = 1.5
def parse(self, response):
resp_list = response.css('#gvshow > tr > td::text').extract()
self.new_list = []
i = 0
while i < len(resp_list):
if i == 0:
pass
elif i % 2 == 1:
new_sub_list = (resp_list[i], resp_list[i+1])
self.new_list.append(new_sub_list)
i = i + 3
i += 1
print(self.new_list)
yield response.follow(self.form_url, self.parse_two)
def parse_two(self, response):
print("Parsing parse", self.form_dict)
yield scrapy.FormRequest(
self.form_url,
formdata={
'__EVENTTARGET': 'ddlexamtype',
'__EVENTARGUMENT':'',
'__LASTFOCUS': '',
'__VIEWSTATE': response.css('input#__VIEWSTATE::attr(value)').extract_first(),
'__VIEWSTATEGENERATOR': response.css('input#__VIEWSTATEGENERATOR::attr(value)').extract_first(),
'__EVENTVALIDATION': response.css('input#__EVENTVALIDATION::attr(value)').extract_first(),
'ddlcollege':str(self.form_dict['collegecode']),
'ddlexamtype':str(self.form_dict['examtype']),
'ddlstream':'<-----Select----->',
'txtrollno':'',
'txtname':''
},
callback=self.parse_scheme
)
def parse_scheme(self, response):
print("Parsing parse_scheme")
yield scrapy.FormRequest(
self.form_url,
formdata={
'__EVENTTARGET': 'ddlexamflag',
'__EVENTARGUMENT':'',
'__LASTFOCUS': '',
'__VIEWSTATE': response.css('input#__VIEWSTATE::attr(value)').extract_first(),
'__VIEWSTATEGENERATOR': response.css('input#__VIEWSTATEGENERATOR::attr(value)').extract_first(),
'__EVENTVALIDATION': response.css('input#__EVENTVALIDATION::attr(value)').extract_first(),
'ddlcollege':str(self.form_dict['collegecode']),
'ddlexamtype':str(self.form_dict['examtype']),
'ddlexamflag':str(self.form_dict['examscheme']),
'ddlstream':'<-----Select----->',
'ddlpart':'<-----Select----->',
'txtrollno':'',
'txtname':''
},
callback=self.parse_stream
)
def parse_stream(self, response):
print("Parsing parse_stream")
yield scrapy.FormRequest(
self.form_url,
formdata={
'__EVENTTARGET': 'ddlstream',
'__EVENTARGUMENT':'',
'__LASTFOCUS': '',
'__VIEWSTATE': response.css('input#__VIEWSTATE::attr(value)').extract_first(),
'__VIEWSTATEGENERATOR': response.css('input#__VIEWSTATEGENERATOR::attr(value)').extract_first(),
'__EVENTVALIDATION': response.css('input#__EVENTVALIDATION::attr(value)').extract_first(),
'ddlcollege':str(self.form_dict['collegecode']),
'ddlexamtype':str(self.form_dict['examtype']),
'ddlexamflag':str(self.form_dict['examscheme']),
'ddlstream':str(self.form_dict['streamcode']),
'ddlpart':'<-----Select----->',
'txtrollno':'',
'txtname':''
},
callback=self.parse_course
)
def parse_course(self, response):
print("Parsing parse_course")
yield scrapy.FormRequest(
self.form_url,
formdata={
'__EVENTTARGET': 'ddlcourse',
'__EVENTARGUMENT':'',
'__LASTFOCUS': '',
'__VIEWSTATE': response.css('input#__VIEWSTATE::attr(value)').extract_first(),
'__VIEWSTATEGENERATOR': response.css('input#__VIEWSTATEGENERATOR::attr(value)').extract_first(),
'__EVENTVALIDATION': response.css('input#__EVENTVALIDATION::attr(value)').extract_first(),
'ddlcollege':str(self.form_dict['collegecode']),
'ddlexamtype':str(self.form_dict['examtype']),
'ddlexamflag':str(self.form_dict['examscheme']),
'ddlstream':str(self.form_dict['streamcode']),
'ddlcourse':str(self.form_dict['coursecode']),
'ddlpart':'<-----Select----->',
'txtrollno':'',
'txtname':''
},
callback=self.parse_part
)
def parse_part(self, response):
print("Parsing parse_part")
yield scrapy.FormRequest(
self.form_url,
formdata={
'__EVENTTARGET': 'ddlpart',
'__EVENTARGUMENT':'',
'__LASTFOCUS': '',
'__VIEWSTATE': response.css('input#__VIEWSTATE::attr(value)').extract_first(),
'__VIEWSTATEGENERATOR': response.css('input#__VIEWSTATEGENERATOR::attr(value)').extract_first(),
'__EVENTVALIDATION': response.css('input#__EVENTVALIDATION::attr(value)').extract_first(),
'ddlcollege':str(self.form_dict['collegecode']),
'ddlexamtype':str(self.form_dict['examtype']),
'ddlexamflag':str(self.form_dict['examscheme']),
'ddlstream':str(self.form_dict['streamcode']),
'ddlcourse':str(self.form_dict['coursecode']),
'ddlpart':str(self.form_dict['year']),
'txtrollno':'',
'txtname':''
},
callback=self.parse_submit
)
def parse_submit(self, response):
user_data = self.new_list
print("Parsing parse_submit")
for d in user_data:
yield scrapy.FormRequest(
self.form_url,
formdata={
'__EVENTTARGET': '',
'__EVENTARGUMENT':'',
'__LASTFOCUS': '',
'__VIEWSTATE': response.css('input#__VIEWSTATE::attr(value)').extract_first(),
'__VIEWSTATEGENERATOR': response.css('input#__VIEWSTATEGENERATOR::attr(value)').extract_first(),
'__EVENTVALIDATION': response.css('input#__EVENTVALIDATION::attr(value)').extract_first(),
'ddlcollege':str(self.form_dict['collegecode']),
'ddlexamtype':str(self.form_dict['examtype']),
'ddlexamflag':str(self.form_dict['examscheme']),
'ddlstream':str(self.form_dict['streamcode']),
'ddlcourse':str(self.form_dict['coursecode']),
'ddlpart':str(self.form_dict['year']),
'ddlsem':str(self.form_dict['semester']),
'txtrollno':d[0],
'txtname':d[1],
'btnsearch':'Print Score Cart/Transcript'
},
callback=self.parse_results,
meta={'name': d[1]}
)
def parse_results(self, response):
with open('results.csv', 'a+') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
# mylist = response.css('#gvshow > tr > td::text').extract()
new_list = []
tr_list = []
for l in response.css('#gvshow tr'):
if l.css('td'):
td_list = []
for td in l.css('td'):
# print(td.css('::text').extract())
td_list.append(td.css('::text').extract_first())
tr_list += td_list
item = response.meta.get('name')
new_list.append(item)
for ele in tr_list:
if ele is not None:
if '/' in ele:
ele = ele.split('/')[0]
if '*' in ele:
ele = ele.split('*')[1]
new_list.append(ele)
else:
new_list.append(ele)
new_tr_list = []
for tr in response.css('#gvrslt tr'):
if tr.css('td'):
new_td_list = tr.css('td::text').extract()
new_tr_list.append(new_td_list[1])
new_list += new_tr_list
wr.writerow(new_list)
if __name__ == '__main__':
process = CrawlerProcess()
process.crawl(SpidyResults)
process.start()