前言
现在手动抢球场真的很难,是时候解放双手了.
Talk is cheap, show me the code.
不想看code也不要紧,直接上我已经做好的服务亦可一键预定https://bookgym.wofost.com/
需要的库:conda 自带库,execjs, xmls
一、实现步骤
这里给大家贴一个前辈的探索,https://blog.csdn.net/baiguikai/article/details/101036360,这边前辈的方案已经过时了,导致无法登陆问题。
新思路:
1、登陆(跳转信息门户; 使用pyexecjs加密密码;获取token;返回登陆运动场预约系统;获取用户ID【不是学号!】)
2、查询空闲场地
3、提交
二、实现方案
1.引入库
代码如下(以武大运动场预约系统为例):
安装package
pip install pyexecjs
pip install lxml
import requests
from lxml import etree
import execjs
import datetime
import json
import time
import numpy as np
2.登陆模块
代码如下(示例):
def login(self):
# 参数
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
}
login = 'https://cas.whu.edu.cn/authserver/login?service=http://gym.whu.edu.cn:80/wechat/autoLoginConnector.jsp'
# 使用会话保持 cookie
s = requests.Session()
# 首次请求,获取隐藏参数
start_response = s.get(login)
start_html = etree.HTML(start_response.text, parser=etree.HTMLParser())
lt = start_html.xpath('//*[@id="casLoginForm"]/input[1]/@value')[0]
dllt = start_html.xpath('//*[@id="casLoginForm"]/input[2]/@value')[0]
execution = start_html.xpath('//*[@id="casLoginForm"]/input[3]/@value')[0]
_eventId = start_html.xpath('//*[@id="casLoginForm"]/input[4]/@value')[0]
rmShown = start_html.xpath('//*[@id="casLoginForm"]/input[5]/@value')[0]
pwdDefaultEncryptSalt = start_html.xpath('//*[@id="casLoginForm"]/input[6]/@value')[0]
# 调用 JavaScript 对密码加密
with open('encrypt.js', 'r',encoding='utf-8') as f:
js = f.read()
ctx = execjs.compile(js)
password = ctx.call('encryptAES', self.password, pwdDefaultEncryptSalt)
# 登录
data = {
'username': self.username,
'password': password,
'lt': lt,
'dllt': dllt,
'execution': execution,
'_eventId': _eventId,
'rmShown'