一、先说背景
大学生论文求助二,因为是需要在实验室利用仪器测验结果以完成论文,所以在预约设备的使用时间上是个难题!不是约不到就是约不到还被导师骂,真是大怨种,没办法因为真的很难,几乎不亚于春运票!
1.1、来自闲鱼的求助
题主不知道这样做是对是错,但如果要是把服务器打挂了,是不是要负法律责任?好像是这样的,之前有过这样的新闻,不过…题主想要留一手,应该是可以办到。
二、需求分析
一开始沟通,题主以为是雇主想通过模拟操作web端实现预约的行为,结果就是一顿操作最后得到的结果是耗时太长,比人工还长,可能就会失手被别人捷足先登,所以题主仔细研究,还是得仔细一点,枉做了前面许多无用功;决定通过API的形式来预约仪器。
2.1、仪器ID
它是唯一不变的ID,这个是每个设备固定的ID。
2.2、时间
这个就比较恶心了,它是每十分钟为一个时段预约,假如要预约上午9点30到17点30分就需要48个时间点,所以需要自己手动生成,否则就是复制参数修改日期即可。
# 预约日期
base_date = "2022-07-01"
subx_date = "{} {}:{}:00"
date_list = []
def generate_time_list():
"""生成预约时间段:list --> str"""
for h in range(9, 18):
if h == 9:
h = "".join(["0", str(h)])
for m in range(0, 59, 10):
if m == 0:
a = subx_date.format(base_date, h, str(m) * 2)
else:
a = subx_date.format(base_date, h, m)
date_list.append(a)
AppointmentTimes = ",".join(date_list[3:-3])
return AppointmentTimes
2.3、登录态
在F12分析过程中发现,它不是以登录返回token这样的形式保持登录,而是通过cookie来储存的,毕竟是web端,这样做没有问题,所以它需要cookie,因为单纯的登录还有验证码,这需要识别图片验证码技术,比较繁琐不愿意浪费时间在这上面,所以选择复制登录后的cookie即可。
# 登录cookies;过期后需要登录再次更新cookie,浏览器打开F12-->network正常的http请求,在requests header处复制cookie的值
cookie = "csrftoken=835328320; CULTURE=zh-CN; bn_theme_style=jinan; .AspNetCore.Session=CfDJ8DKTn8Q111g95NRrGwYnoItkylVnyV1EB4cDINYXeP/Zu0c3xlEt0XXJqGOIG4S7W93ZE+gSpswUnvZTlP49yJEkZZEE+VSlWEQC4WvkKY1jM0MG3UMCzXlnc3uOW4HfoqvySk8hgX/a+SrWJg3CGWRGABCJItPnQaxft0y6q; .AspNetCore.Cookies=CfDJ8DKTn8Q-D8lJhg95NRrGwYkAZmXiJuVm-p1DhTyvaZiiQ7nfSq6YFZceS4PHYj111ZuWUl11tCZv0SE0OAajkVn9ZUPk85QRqagurWzsEU-tadi-NEJnKdzgN9ffJoc5OYEp8wPSYOL4LAN3CnpDgGAIOdsrsn7diiOhulO-GZkRBzJZpwtghcwiWHfju4cyx6rEJGpQrdR-0xBe3GSKmDt8lKKr_lD-HtXEWMMLbVompyA_JhkXlKUQKOohq5JTgvffEHB2SVanXm2ePzHkKbEX3pffrXA_6Tb3gTF8aZs_TQzoPKBDsQHdr4Y808BS1A"
cookies = {
}
coo = RequestsCookieJar()
for cook in cookie.split(";"):
co = cook.strip().split("=")
cookies[co[0]] = co[1] # 使用grequests库做请求时需要的cookies
coo.set(co[0], co[1]) # http请求set登录态
2.4、实现步骤
前面已经获取了API、入参、cookies;剩下来的操作就是模拟用户行为了,多测试几遍是否可行!不行,又是哪里出了问题,所以题主需要在这里把问题都摸清了。然后按部就班完成一次测试,代表代码和逻辑实现没有问题。
2.5、完整的代码
import time
import requests
from requests.cookies import RequestsCookieJar
# 预约地址
url = "https://xxxxxx.edu.cn/xxsole/xxxtment/xxxpointment"
# 预约日期
base_date = "2022-07-01"
subx_date = "{} {}:{}:00"
date_list = []
def generate_time_list():
"""生成预约时间段:list --> str"""
for h in range(9, 18):
if h == 9:
h = "".join(["0", str(h)])
for m in range(0, 59, 10):
if m == 0:
a = subx_date.format(base_date, h, str(m) * 2)
else:
a = subx_date.format(base_date, h, m)
date_list.append(a)
AppointmentTimes = ",".join(date_list[3:-3])
return AppointmentTimes
# 构造入参
payload = {
"EquipmentId": "da7xxxx607-1e2xx659-baxxxa7xxxx2a", # 设备id
"ChangeAppointmentId": None,
"AppointmentUserId": None,
"VirtualEquipmentBindId": None,
"WeekIndex": None,
"AppointmentStep": 10,
"bookingTime_1": base_date, # 预约日期
"SubjectId": "12dxx3-4ae6-xxxx-3cdd1xxxx74", # 科研主题
"SubjectProjectId": None,
"UseNature": 0,
"ExperimentationContent": 1,
"SampleNo": None,
"SampleCount": 0,
"SampleStuff": None,
"SampleSize": None,
"Target": None,
"hasReadTheNotice": "on",
"AppointmentTimes": generate_time_list()} # 预约时间
# 登录cookies;过期后需要登录再次更新cookie,浏览器打开F12-->network正常的http请求,在requests header处复制cookie的值
cookie = "csrftoken=835xxxx8320; CULTURE=zh-CN; bn_theme_style=xxxn; .AspNetCore.Session=CfDx223n8Q+D8lJhg95NRrGwYnoItkylVnyV1EB4cDINYXeP/Zu0c3xlEt0XXJqGOIG4S7W93ZE+gSpswUnvZTlP49yJEkZZEE+VSlWEQC4WvkKY1jM0MG3UMCzXlnc3uOW4HfoqvySk8hgX/a+SrWJg3CGWRGABCJItPnQaxft0y6q; .AspNetCore.Cookies=CfDJ8DKTn8Q-D8lJhg95NRrGwYkAZmXiJuVm-p1DhTyvaZiiQ7nfSq6YFZceS4PHYjXplZuWUl11tCZv0SE0OAajkVn9ZUPk85QRqagurWzsEU-tadi-NEJnKdzgN9ffJoc5OYEp8wPSYOL4LAN333gGAIOdsrxxxiOhulO-GZkRBzJZpwtghcwiWHfju4cyx6rEJGpQrdR-0xBe3GSKmDt8lKKr_lD-HtXEWMMLbVompyA_JhkXlKUQKOohq5JTgvffEHB2SVanXm2ePzHkKbEX3pffrXA_6Tb3gTF8aZs_TQzoxxxHdr4Y808BS1A"
cookies = {
}
coo = RequestsCookieJar()
for cook in cookie.split(";"):
co = cook.strip().split("=")
cookies[co[0]] = co[1] # 使用grequests库做请求时需要的cookies
coo.set(co[0], co[1]) # http请求set登录态
# 模拟请求头信息
headers = {
"content-type": "application/x-www-form-urlencoded; charset=UTF-8", 'Connection': 'close'}
headers[
"Referer"] = "https://xxxxxx/Coxxx/Apxxxtment/ApxxxxentBoxContainer?id=da796xxxxxc1a7372a"
headers["Origin"] = "https://xxxxx.edu.cn"
headers["Host"] = "xxxxx.edu.cn"
headers[
"User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36"
# http session请求对象
sess = requests.session()
sess.cookies.update(coo) # 刷新cookie
while True: # 预约前,不断循环
start_time = time.time()
res = sess.post(url=url, data=payload, headers=headers)
if "预约成功" in res.text and "xxx" in res.text:
print("恭喜你,预约成功!!!")
break # 预约成功跳出循环
elif "暂时不可预约" in res.text:
print(res.text)
continue # 经过测试每5s才成功请求一次
else:
print(res.text) # 预约失败
spent_time = time.time() - start_time
print("花费是时间:{}s".format(spent_time))
三、扩展
- 想通过gevent协程并发来解决预约问题,不管并发多少次,每次都只会成功一次,接下来的并发8次都会失败,所以不采用这种损人不利己的方式。
import grequests
from requests.cookies import RequestsCookieJar
# 登录cookies;过期后需要登录再次更新cookie,浏览器打开F12-->network正常的http请求,在requests header处复制cookie的值
cookie = "csrftoken=835328320; CULTURE=zh-CN; bn_theme_style=jinan; .AspNetCore.Session=CfDJ8DKTn8Q+D8lJhg95NRrGwYnoItkylVnyV1EB4cDINYXeP/Zu0c3xlEt0XXJqGOIG4S7W93ZE+gSpswUnvZTlP49yJEkZZEE+VSlWEQC4WvkK211122UMCzXlnc3uOW4HfoqvySk8hgX/a+SrWJg3CGWRGABCJItPnQaxft0y6q; .AspNetCore.Cookies=CfDJ8DKTn8Q-D8lJhg95NRrGwYkAZmXiJuVm-p1DhTyvaZiiQ7nfSq6YFZceS4PHYjXplZuWUl11tCZv0SE0OAajkVn9ZUPk85QRqagurWzsEU-tadi-NEJnKdzgN9ffJoc5OYEp8wPSYOL4LAN3CnpDgGAIOdsrsn7diiOhulO-GZkRBzJZpwtghcwiWHfju4cyx6rEJGpQrdR-0xBe3GSKmDt8lKKr_lD-HtXEWMMLbVompyA_JhkXlKUQKOohq5JTgvffEHB2SVanXm2ePzHkKbEX3pffrXA_6Tb3gTF8aZs_TQzoPKBDsQHdr4Y808BS1A"
cookies = {
}
coo = RequestsCookieJar()
for cook in cookie.split(";"):
co = cook.strip().split("=")
cookies[co[0]] = co[1] # 使用grequests库做请求时需要的cookies
start_time = time.time()
for _ in range(3):
req_list = [grequests.post(url=url, data=payload,cookies=cookies, headers=headers) for _ in range(20)]
res_list = grequests.map(req_list)
for res in res_list:
print(res.text)
spent_time = time.time() - start_time
print("花费是时间:{}s".format(spent_time))
四、总结
从时间成本上来算,是亏了,首先误以为是ui自动化可以达到目的,效果有了但是不一定能成功,所以题主痛定思痛,需要认真思考、分析雇主的需求,然后再决定实现方案,免得最后雇主会因不满意而拒绝买单。