-
Notifications
You must be signed in to change notification settings - Fork 25
/
scrape.py
148 lines (130 loc) · 4.83 KB
/
scrape.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
#!/usr/bin/env python3
from django_extensions.management.jobs import DailyJob
import standalone
standalone.run("lifeiitkbackend.settings")
from users.models.users import User
from bs4 import BeautifulSoup
import re
import requests
s = requests.Session()
s.get("https://oa.cc.iitk.ac.in/Oa/Jsp/Main_Frameset.jsp")
s.get("https://oa.cc.iitk.ac.in/Oa/Jsp/Main_Intro.jsp?frm='SRCH'")
s.get("https://oa.cc.iitk.ac.in/Oa/Jsp/OAServices/IITK_Srch.jsp?typ=stud")
headers = {
"Referer": "https://oa.cc.iitk.ac.in/Oa/Jsp/OAServices/IITK_Srch.jsp?typ=stud"
}
headers1 = {
"Referer": "https://oa.cc.iitk.ac.in/Oa/Jsp/OAServices/IITk_SrchStudRoll_new.jsp"
}
payload = {
"k4": "oa",
"numtxt": "",
"recpos": 0,
"str": "",
"selstudrol": "",
"selstuddep": "",
"selstudnam": "",
"txrollno": "",
"Dept_Stud": "",
"selnam1": "",
"mail": "",
}
payload1 = {"typ": ["stud"] * 12, "numtxt": "", "sbm": ["Y"] * 12}
TOTAL = 8385
r = s.post(
"https://oa.cc.iitk.ac.in/Oa/Jsp/OAServices/IITk_SrchStudRoll_new.jsp",
headers=headers,
data=payload,
)
soup = BeautifulSoup(r.text, "html.parser")
class Job(DailyJob):
def process_response_soup(self, soup):
for link in soup.select(".TableText a"):
roll = link.get_text().strip()
payload1["numtxt"] = roll
r1 = s.post(
"https://oa.cc.iitk.ac.in/Oa/Jsp/OAServices/IITk_SrchRes_new.jsp",
headers=headers1,
data=payload1,
)
soup1 = BeautifulSoup(r1.text, "html.parser")
name = ""
program = ""
dept = ""
hall = ""
room = ""
username = ""
blood_group = ""
gender = ""
hometown = ""
for para in soup1.select(".TableContent p"):
body = para.get_text().strip()
field = body.split(":")
key = field[0].strip()
value = field[1].strip()
if key == "Name":
name = value.lower().title()
elif key == "Program":
program = value
elif key == "Department":
dept = value.lower().title()
elif key == "Hostel Info":
if len(value.split(",")) > 1:
hall = value.split(",")[0].strip()
room = value.split(",")[1].strip()
elif key == "E-Mail":
if len(value.split("@")) > 1:
username = value.split("@")[0].strip()
elif key == "Blood Group":
blood_group = value
elif key == "Gender":
if len(value.split("\t")) > 1:
gender = value.split("\t")[0].strip()
else:
print("{} {}".format(key, value))
body = soup1.prettify()
if len(body.split("Permanent Address :")) > 1:
address = body.split("Permanent Address :")[1].split(",")
if len(address) > 2:
address = address[len(address) - 3 : len(address) - 1]
hometown = "{}, {}".format(address[0], address[1])
image = (
"http://oa.cc.iitk.ac.in/Oa/Jsp/Photo/" + str(roll) + "_0.jpg"
)
if len(User.objects.filter(roll=roll)) == 0:
u = User(
roll=roll,
username=username,
image=image,
name=name,
program=program,
dept=dept,
hall=hall,
room=room,
blood_group=blood_group,
gender=gender,
hometown=hometown,
)
u.save()
def execute(self, soup):
for link in soup.select(".DivContent"):
substituted = re.sub(r"\s+", " ", link.text)
pattern = re.compile(
r"\s*You are viewing 1 to 12 records out of (\d+) records\s*"
)
match = pattern.match(substituted)
TOTAL = int(match.group(1))
print("Total: {}".format(TOTAL))
self.process_response_soup(soup)
for i in range(12, TOTAL + 1, 12):
payload["recpos"] = i
r = s.post(
"https://oa.cc.iitk.ac.in/Oa/Jsp/OAServices/IITk_SrchStudRoll_new.jsp",
headers=headers,
data=payload,
)
soup = BeautifulSoup(r.text, "html.parser")
self.process_response_soup(soup)
print("Processed {}".format(i + 12))
x = Job()
x.execute(soup)