工大助手(加权成绩计算)

加权成绩计算

组内讨论

  1. 图形界面tab键有误
  2. 图形界面改进
  3. 加权成绩计算有误

改进

根据学校官网给出的加权计算方法,重新调整了爬虫需要爬取的数据,例如将重修标记爬取到本地,之前认为辅修课程计入加权平均分的计算,所以并没有将标识重修的部分爬取下来,并保存在excel中,这样就可以在客户端同时展示成绩并且在计算加权的时候去掉这部分的成绩。

修改之后的代码:

	def login():
        try:
                page = urllib2.urlopen(baseUrl).read()
                soup = BeautifulSoup(page,"html.parser")
                tmp = soup.find('input',attrs={'name':'__VIEWSTATE'})
                viewstate = tmp['value']
                studentNo = raw_input("studentNo:")
                studentPass = raw_input("studentPass:")
                secretCode = raw_input("secretCode:")
                postData = urllib.urlencode({
                        '__VIEWSTATE': viewstate,
                        'txtUserName': studentNo,
                        'TextBox2': studentPass,
                        'txtSecretCode': secretCode,
                        'RadioButtonList1': '学生',
                        'Button1': '',
                        'lbLanguage': '',
                        'hidPdrs': '',
                        'hidsc': ''
                })
                request = urllib2.Request(baseUrl, postData, headers)
                result = opener.open(request)
                soup = BeautifulSoup(result.read(),"html.parser")
                tmp = soup.find(id="xhxm")

                studentName = str(tmp.string.decode('utf-8')[:-2])
                graduURL1 = "http://gdjwgl.bjut.edu.cn/xscjcx.aspx?xh=" + studentNo + "&xm=" + studentName + "&gnmkdm=N121605"
                referer = "http://gdjwgl.bjut.edu.cn/xs_main.aspx?xh=" + studentNo
                graduURL1 = urllib.quote(graduURL1,"?&/=:")

                headers_gra1 = {'Referer':referer,'user-Agent': user_agent,'Host':'gdjwgl.bjut.edu.cn',
                                    'Accept-Encoding': 'gzip, deflate','Connection': 'Keep-Alive'}
                headers_gra2 = {'Referer': graduURL1,'user-Agent': user_agent}
                
                request_gra1 = urllib2.Request(graduURL1, headers=headers_gra1)
                result = opener.open(request_gra1)
                soup = BeautifulSoup(result.read(),"html.parser")
                tmp = soup.find('input',attrs={'name':'__VIEWSTATE'})
                viewstate = tmp['value']
                postData_Gra = urllib.urlencode({
                        '__EVENTTARGET':'',
                        '__EVENTARGUMENT':'',
                        'btn_zcj':'历年成绩',
                        '__VIEWSTATE':viewstate,
                        'hidLanguage': '',
                        'ddLXN':'',
                        'ddLXQ':'',
                        'ddl_kcxz':''
                })
                request_gra2 = urllib2.Request(graduURL1, postData_Gra, headers_gra2)
                result = opener.open(request_gra2)
                return result.read()
        except urllib2.URLError, e:
                if hasattr(e,"code"):
                        return e.code

def writeIntoExcel():
        pageCode = login()
        # print pageCode
        
        soup = BeautifulSoup(pageCode, 'html.parser')

        table = soup.find("table", class_="datelist")
       
        book = xlwt.Workbook(encoding="utf-8", style_compression=0)
        sheet = book.add_sheet("score", cell_overwrite_ok=True)

        trs = table.find("tr")
        tds = trs.find_all("td")
        #print tds
        col = 0
        
        for i in range(len(tds)):
            if i == 0 or i == 1 or i == 3:
                sheet.write(0, col, tds[i].find('a').string.decode("utf-8"))
                col += 1
            if i == 4 or i == 6 or i == 7 or i == 8 or i == 9:
                sheet.write(0, col, tds[i].string.decode("utf-8"))
                col += 1

        row = 0
        trs = table.find_all("tr")
        for i in range(len(trs)):
            if i > 0:
                tds = trs[i].find_all("td")
                row += 1
                col = 0
                for j in range(len(tds)):
                    if j == 0 or j == 1 or j == 3 or j == 4 or j == 6 or j == 7 or j == 8 or j == 9:
                        sheet.write(row, col, tds[j].string.decode("utf-8"))
                        col += 1

        book.save("score.xls")
        print "EXCEL done!"

posted on 2016-06-29 23:30  13070046孙宇辰  阅读(449)  评论(0编辑  收藏  举报

导航