Created
March 16, 2013 02:48
-
-
Save vincenting/5174710 to your computer and use it in GitHub Desktop.
根据学号和密码获取姓名、性别、宿舍号,失败返回false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = 'Vincent Ting' | |
import urllib2, urllib, re, cookielib | |
def CCNUAuth(su_id=None, su_psw=None, get_name=False): | |
""" | |
根据学号和密码获取姓名、性别、宿舍号,失败返回false | |
:param su_id: | |
:param su_psw: | |
:return: | |
""" | |
if not (su_id and su_psw): | |
return False | |
cookie = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) | |
opener.addheaders = [('Connection', 'Keep-Alive')] | |
opener.addheaders = [('Cache-Control', 'no-cache')] | |
url_login = "http://portal.ccnu.edu.cn/loginAction.do" | |
body = (('userName', su_id), ('userPass', su_psw)) | |
if len(opener.open(url_login, urllib.urlencode(body)).read()) != 53: | |
return False | |
if not get_name: | |
return True | |
auth_result = opener.open('http://portal.ccnu.edu.cn/index_jg.jsp').read() | |
name = re.compile("(.*?) ").findall(auth_result)[1].decode('gb2312').encode('utf8').replace(' ', '') | |
opener.open("http://portal.ccnu.edu.cn/roamingAction.do?appId=HSXG") | |
info = opener.open("http://202.114.32.143/ccnuxg/xg/studentInfo.do?method=getStudentInfo").read().decode('GBK') | |
sex = re.compile("<td height=\"21\"> (.*?)</td>").findall(info)[0] | |
room = re.compile("<td> (.*?)</td>").findall(info)[14] | |
return { | |
'name': name, | |
'sex': sex, | |
'room': room | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment