1+ import requests
2+ from lxml import html
3+ from urlparse import urlparse
4+ from bs4 import BeautifulSoup
5+ import json
6+
7+ if __name__ == '__main__' :
8+ s = requests .Session ()
9+ r = s .get (url = "https://paco.ua.pt/secvirtual/c_historiconotas.asp" )
10+
11+ username = "EMAIL"
12+ password = "PASSWORD"
13+
14+ tree = html .fromstring (r .content )
15+ o = urlparse (r .url )
16+
17+ url_auth = o .scheme + "://" + o .netloc + tree .body .forms [0 ].attrib ["action" ]
18+ r = s .post (url_auth , data = {'j_password' : password , 'j_username' : username , 'Submeter' : 'OK' })
19+
20+ tree = html .fromstring (r .content )
21+
22+ inputs = tree .xpath ("//input" )
23+
24+ payload = {'RelayState' : inputs [0 ].value , 'SAMLResponse' : inputs [1 ].value }
25+
26+ r = s .post (tree .body .forms [0 ].attrib ["action" ], data = payload )
27+ r = s .get ("https://paco.ua.pt/secvirtual/c_planocurr.asp" )
28+
29+ soup = BeautifulSoup (r .content )
30+ table = soup .find ('table' , attrs = {'width' :'95%' , 'align' :'center' , 'cellspadding' :'2' })
31+
32+ cadeiras = []
33+
34+ for row in table .findAll ("tr" ):
35+ cells = row .findAll ("td" )
36+ if len (cells ) == 8 and cells [1 ].text .rstrip ().replace ("\r \n \t " , "" ) != 'Codigo' :
37+ if len (cells [7 ].text .rstrip ().replace ("\r \n \t " , "" )) != 0 :
38+ cadeiras += [{'codigo' : int (cells [1 ].text .rstrip ().replace ("\r \n \t " , "" )),
39+ 'nome' : cells [2 ].text .rstrip ().replace ("\r \n \t " , "" ),
40+ 'ano' : int (cells [3 ].text .rstrip ().replace ("\r \n \t " , "" )),
41+ 'semestre' : int (cells [4 ].text .rstrip ().replace ("\r \n \t " , "" )),
42+ 'ects' :float (cells [6 ].text .rstrip ().replace ("\r \n \t " , "" ).replace ("," , "." )),
43+ 'nota' : float (cells [7 ].text .rstrip ().replace ("\r \n \t " , "" ).replace ("," , "." ))}]
44+ nota = 0.0
45+ creditos = 0.0
46+
47+ for cadeira in cadeiras :
48+ nota += (cadeira ["nota" ] * cadeira ["ects" ])
49+ creditos += cadeira ["ects" ]
50+
51+ nota = nota / creditos
52+
53+ semestres_ano = []
54+
55+ for cadeira in cadeiras :
56+ if {"ano" : cadeira ["ano" ], "semestre" : cadeira ["semestre" ]} not in semestres_ano :
57+ semestres_ano += [{"ano" : cadeira ["ano" ], "semestre" : cadeira ["semestre" ]}]
58+
59+ # notas por semestre e ano
60+ for semestre in semestres_ano :
61+ semestre ["ects" ] = 0
62+ semestre ["nota" ] = 0
63+ for cadeira in cadeiras :
64+ if cadeira ["ano" ] == semestre ["ano" ] and cadeira ["semestre" ] == semestre ["semestre" ]:
65+ semestre ["ects" ] += cadeira ["ects" ]
66+ semestre ["nota" ] += (cadeira ["nota" ] * cadeira ["ects" ])
67+ semestre ["nota" ] /= semestre ["ects" ]
68+
69+ print "ECTS: " + str (creditos )
70+ print "Nota: " + str (nota )
71+ print "N. de Cadeiras: " + str (len (cadeiras ))
72+ # print json.dumps({'cadeiras': cadeiras, 'media': nota, 'semestres': semestres_ano})
0 commit comments