用Python写了个URP查分程序

将两个文件放在同一目录中,修改gpa.sh中的账号密码,并用chmod设置为可执行,运行即可。
写得不怎么样,像URPParser里处理标签的时候直接输出了,很不规范,不过懒得改了
urpparser.py:
from  sgmllib  import  SGMLParser

class  URPParser(SGMLParser):
    
def  reset(self):
        self.tdOpen 
=  0
        self.colCount 
=   - 1
        self.firstRow 
=   1
        self.pieces 
=  []
        SGMLParser.reset(self)

    
def  start_td(self, attrs):
        
"""
            When encountered with tag td, check whether there's
            an align property in the tag, which will distinguish
            score table from others.
        
"""
            
        
for  (k, v)  in  attrs:
            
if  (k  ==   " align " ):
                self.tdOpen 
=   1
                
break

    
def  end_td(self):
        self.tdOpen 
=  0

    
def  handle_data(self, text):
        
if  (self.tdOpen  >  0):
            
if  (len(text.strip())  >  0):
                self.colCount 
+=   1
                
if  (self.colCount  >   6 ):
                    self.colCount 
=  0
                    self.firstRow 
=  0
                    
print
                
if  (self.firstRow):
                    
return
                
if  (self.colCount  ==   2 ):
                    
print   " \t " ,
                
else :
                    
print  text.strip(), " \t " ,

gpa.sh:
# !/usr/bin/python
import  urllib, cookielib, urllib2

loginURL 
=   " http://fdis.fudan.edu.cn:58080/amserver/UI/Login? "   + \
           
" goto=http%3A%2F%2Fwww.urp.fudan.edu.cn%3A84%2Feps "   + \
           
" tar%2Fapp%2Ffudan%2FframeSub.jsp%3FaffairNO%3D035067 "
scoreURL 
=   " http://www.urp.fudan.edu.cn:84/epstar/app/fudan/S "   + \
           
" coreManger/ScoreViewer/Student/Course.jsp "
logoutURL 
=   " http://www.urp.fudan.edu.cn/logout.jsp "

cookie 
=  cookielib.CookieJar()
opener 
=  urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
str 
=  urllib.urlencode({ ' Login.Token1 ' ' 06301000000 ' ' Login.Token2 ' " yourpassword " })
sock1 
=  urllib2.urlopen(loginURL, str)
loginHTML 
=  sock1.read()
sock1.close()

sock2 
=  urllib2.urlopen(scoreURL)
scoreHTML 
=  sock2.read()
sock2.close()

sock3 
=  urllib2.urlopen(logoutURL)
sock3.close()

from  urpparser  import  URPParser
parser 
=  URPParser()
parser.feed(scoreHTML)
print



你可能感兴趣的:(用Python写了个URP查分程序)