用python实现超星学习通自动签到

本文最后更新于:2020年6月9日 上午

依赖库

  • requests
  • pillow
  • lxml

使用方法

1.测试环境为python3.7+,自行安装python3
2.安装所需第三方模块,依次执行pip install requestspip install pillowpip install lxml
3.Python 和需要模块都装好了直接在目录 cmd 运行所需脚本。

脚本代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import requests,time,json,re,base64
from io import BytesIO
requests.packages.urllib3.disable_warnings()
from PIL import Image,ImageDraw,ImageChops
from lxml import etree
from urllib.parse import urlparse, parse_qs

username = "" #登录账号
password = "" #登录密码
sckey = "" #Server酱推送提醒key

s = requests.Session()
s.headers.update({'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'})
activates = []

def sign(aid, uid):
global activates
url = "https://mobilelearn.chaoxing.com/pptSign/stuSignajax?activeId=" + aid + "&uid=" + uid + "&clientip=&latitude=-1&longitude=-1&appType=15&fid=0"
res = s.get(url,verify=False)
if (res.text == "success"):
print(" 签到成功!")
if sckey != "":
data = {
"text" : "签到成功",
"desp" : "aid" + str(aid)
}
sckeyurl = "http://sc.ftqq.com/"+str(sckey)+".send"
web = s.post(sckeyurl, data=data, verify=False)
activates.append(aid)
elif(res.text == "您已签到过了"):
print(res.text)
activates.append(aid)
else:
if sckey != "":
data = {
"text" : "签到失败",
"desp" : "aid" + str(aid)
}
sckeyurl = "http://sc.ftqq.com/"+str(sckey)+".send"
web = s.post(sckeyurl, data=data, verify=False)
print("签到失败")



def login():
global uid,username,password
if(username == "" or password == ""):
username = input("登录账号:")
password = input("登录密码:")
#旧接口,已失效
#url="http://i.chaoxing.com/vlogin?passWord="+str(password)+"&userName="+str(username)
url = f'https://passport2-api.chaoxing.com/v11/loginregister?uname='+str(username)+'&code='+str(password)
res= s.get(url)
if("验证通过" in str(res.text)):
print('Login success!')
for key, value in res.cookies.items():
if key=="_uid":
uid=value
return s
else:
print(username,password)
print('账号密码有误,请重试。')
username = ""
password = ""
login()




def getuserdata():
web = s.get('http://mooc1-1.chaoxing.com/visit/courses')
h1 = etree.HTML(web.text)
name = h1.xpath('//h3[@class = "clearfix"]/a/text()')
print("-----------课程名称-----------")
print(name)
print("------------------------------")
global count
try:
count
except NameError:
count_exist = False
else:
count_exist = True
if(count_exist):
pass
else:
if(len(name) == 1):
count = 0
else:
#count = 0
count = int(input("请用数字选择要签到的课程(从0开始):"))
geturl = h1.xpath('//div[@class = "Mcon1img httpsClass"]/a/@href')
i = 0
courseurl = []
for temp in range(0,len(geturl)):
if("/mycourse/studentcourse" in geturl[i]):
courseurl.append(geturl[i])
i += 1
url = 'https://mooc1-1.chaoxing.com' + courseurl[count]
url_query = urlparse(url).query
userdata = dict([(k, v[0]) for k, v in parse_qs(url_query).items()])
global courseId, classId
courseId = userdata["courseId"]
classId = userdata["clazzid"]

def main():
getuserdata()
while 1:
url = f'https://mobilelearn.chaoxing.com/ppt/activeAPI/taskactivelist?courseId='+courseId+'&classId='+classId+'&uid='+str(uid)
web = s.get(url,verify=False)
activeList = json.loads(web.text)["activeList"]
for item in activeList:
if ("nameTwo" not in item):
continue
if (item['activeType'] == 2 and item['status'] == 1):
signurl = item['url']
var = signurl.split("&")
for var1 in var:
var2 = var1.split("=")
if(var2[0] == "activePrimaryId"):
aid = var2[1]
if (aid not in activates):
print("查询到签到 名称:%s 活动时间:%s aid:%s" % (
item['nameOne'], item['nameFour'], aid))
sign(aid, uid)
else:
time.sleep(3)
print("签到暂未开通")

if __name__ == "__main__":
print("登录成功后等待签到开始")
try:
#captchalogin(username,password)
login()
main()
except Exception as e:
print('repr(e):', repr(e))

注意事项

1.需要手机号登录,无fid信息无法使用院校学号登录
2.如果需要使用学号登录,自己或许院校fid信息,并修改代码中的fid信息
3.如果首次登录提示错误,关闭程序再打开试试
4.如果抓不到课程信息什么的,可能需要自己查询html自己修改xpath

帮助

https://github.com/mengshouer/mooc_tools