Skip to content

Instantly share code, notes, and snippets.

@Miyayx
Last active December 23, 2015 12:09
Show Gist options
  • Save Miyayx/6633491 to your computer and use it in GitHub Desktop.
Save Miyayx/6633491 to your computer and use it in GitHub Desktop.
To choose course quickly in tsinghua
#!/usr/bin/env python2.7
#encoding=utf-8
import md5
import time
import urllib
import urllib2
import cookielib
from bs4 import BeautifulSoup
host = 'zhjwxk.cic.tsinghua.edu.cn'
#JSESSIONID = 'adb5XTNBAq-ng-BHZW4eu'
#thuwebcookie = '1006923686.20480.0000'
p_xnxq = '2013-2014-1'
#kch_id = '60230023'
kch_id = '60510082'
kch_num = '200'
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie),urllib2.HTTPHandler)
opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)')]
urllib2.install_opener(opener)
def generateXKRequest():
urlxk = 'http://zhjwxk.cic.tsinghua.edu.cn/xkYjs.vxkYjsXkbBs.do'
paramsxk = {
"m":"selectKc",
"p_xnxq":p_xnxq,
"pathContent":"%D1%A1%BF%CE"
}
paramsxk2 = {
"m":"yxSearchTab",
"p_xnxq":p_xnxq,
"tokenPriFlag":"yx"
}
req = urllib2.Request(urlxk,urllib.urlencode(paramsxk2))
#req.add_header("Referer","http://zhjwxk.cic.tsinghua.edu.cn/xkYjs.vxkYjsXkbBs.do?m=main")
#req.add_header("Referer","http://zhjwxk.cic.tsinghua.edu.cn/xkYjs.vxkYjsXkbBs.do?m=selectKc&p_xnxq=2013-2014-1&pathContent=%D1%A1%BF%CE")
#req.add_header("Host",host)
#req.add_header('Cookie','JSESSIONID='+JSESSIONID+'; thuwebcookie='+thuwebcookie)
return req
def generateRequest(token):
url = 'http://zhjwxk.cic.tsinghua.edu.cn/xkYjs.vxkYjsXkbBs.do'
params = {
"m":"saveXwKc",
"token":token,
"p_xnxq":p_xnxq,
"tokenPriFlag":"xwk",
"tabType":None,
"page":None,
#"p_kch":kch_id,
"p_kch":None,
"p_kcm":None,
"p_xwk_id":p_xnxq+";"+kch_id+";"+kch_num+";"
}
req = urllib2.Request(url,urllib.urlencode(params))
#req.add_header("Referer",url+'?m=xwkSearch&p_xnxq=2013-2014-1&tokenPriFlag=xwk')
#req.add_header("Referer",url)
#req.add_header("Host",host)
#req.add_header('Cookie','JSESSIONID='+JSESSIONID+'; thuwebcookie='+thuwebcookie)
return req
def requestSubmit(req):
resp = urllib2.urlopen(req)
#print unicode(resp.read(),'GBK').encode('utf-8')
return resp.read()
def get_token(page):
soup = BeautifulSoup(page)
form = soup.find('form')
if form:
for i in form.find_all('input'):
if i['name']=='token':
return i['value']
else: return None
def chooseSuccess(page):
soup = BeautifulSoup(page)
try:
script = soup.find('body').find_all('script')[1]
except Exception,e:
print e.message
print page
return
script = str(script)
if script:
msgB = script.index('showMsg')
msgE = script.index(';',msgB)
msg = script[msgB:msgE]
print msg
if msg.find('成功')> -1:
return True
else: return False
def find_pic_url(page):
soup = BeautifulSoup(page)
return 'http://zhjwxk.cic.tsinghua.edu.cn'+soup.find(id="captcha")['src']
def get_captcha(page):
return urllib.urlopen(find_pic_url(page)).read()
def look_up(filename,pic_md5):
with open(filename) as f:
lines = f.readlines()
for l in lines:
s = l.split()
if s[0] == pic_md5:
return s[1]
def xk_login(text):
login_url = 'https://zhjwxk.cic.tsinghua.edu.cn/j_acegi_formlogin_xsxk.do'
params = {
"j_username":"aaa",
"j_password":"bbb",
"captchaflag":"login1",
"_login_image_":text
}
req = urllib2.Request(login_url,urllib.urlencode(params))
#req.add_header("Referer",'http://zhjwxk.cic.tsinghua.edu.cn/xklogin.do')
#req.add_header("Host",host)
#req.add_header('Cookie','JSESSIONID='+login_JSESSIONID+'; thuwebcookie='+login_thuwebcookie)
page = requestSubmit(req)
soup = BeautifulSoup(page)
print soup.find('div')
if soup.find('div'):
#if soup.find('div align="center"'):
#if str(page).find('验证码不正确') > -1:
return False
else:
return True
def login():
login_url = 'http://zhjwxk.cic.tsinghua.edu.cn/xklogin.do'
while True:
req = urllib2.Request(login_url)
print 'Get captcha...'
page = requestSubmit(req)
captcha = get_captcha(page)
m = md5.new(captcha).hexdigest()
print m
text = look_up('bbb.dat', m).strip()
if not text:
continue
print 'Captcha is '+text
if xk_login(text):
start()
break
def start():
start_t = time.time()
req = generateXKRequest()
page = requestSubmit(req)
token = get_token(page)
print 'token = '+str(token)
while True:
if token:
req2 = generateRequest(token)
page = requestSubmit(req2)
#print unicode(page,'GBK').encode('utf-8')
if(chooseSuccess(page)):
return
else:
token = get_token(page)
time.sleep(30)
end_t = time.time()
print end_t-start_t
login()
#start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment