? ? ? ?目前中国许多大学图书馆座位有限,无法容纳全部学生学习要求,所以许多大学的图书馆会采用在网上抢座的形式来分配座位,而抢的人太多了就会导致抢不上,所以我用python实现了图书馆抢座系统,这个系统输入开始时间、结束时间、账号和密码就可以自动抢座。
? ? ? ?在这里我们使用selenium库在模仿登录,登录成功后使用get_cookies()方法就可以获取登录后的cookies,随后在预定座位时带上这个cookies就可以成功预定。我们学校登录需要验证码,验证码是一个计算式,所以我先用tesserocr识别,在计算得到结果。整个实现过程就是这样,具体细节就不过多阐述。代码稍微修改一下就可以使用。
import tesserocr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import base64
import requests
import json
import time
import datetime
from datetime import date, timedelta
# 定义预定函数
def booking(cookie, start, end, start_time, end_time, dev_id):
while True:
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44',
'Cookie': cookie
}
params = {
'dialogid':'',
'dev_id': dev_id,
'lab_id': '',
'kind_id': '',
'room_id': '',
'type': 'dev',
'prop': '',
'test_id': '',
'term': '',
'number': '',
'classkind': '',
'test_name': '',
'start': start,
'end': end,
'start_time': start_time,
'end_time': end_time,
'up_file': '',
'memo': '',
'act': 'set_resv',
'_': '1636532955267'
}
url = '填上预定网址'
re = requests.get(url=url, headers=headers, params=params)
res = json.loads(re.text)
msg = res['msg']
print(msg)
if msg == '操作成功!':
return 1
if msg == '当前时间预约冲突':
return 2
except:
pass
# 传入验证码图片(.png)返回结果
def validate_result(image_url):
image = Image.open(image_url)
validate_formula = list(tesserocr.image_to_text(image))
print(validate_formula)
if len(validate_formula) < 3:
print('验证码读取失败,请等待')
return None
else:
del validate_formula[len(validate_formula) - 1]
del validate_formula[len(validate_formula) - 1]
del validate_formula[len(validate_formula) - 1]
del validate_formula[len(validate_formula) - 1]
validate_formula = ''.join(validate_formula)
#print(validate_formula)
validate_result = eval(validate_formula)
#print(validate_result)
return validate_result
# 定义函数取得cookies
def get_cookie(id, password):
while True:
try:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('__headless')
driver = webdriver.Chrome(options=chrome_options)
driver.get(r'登录页面网址')
time.sleep(10)
# 获取图片base64编码
img_base64 = driver.find_element_by_xpath(r'//div[@class="index-captcha-2FKeU"]/img').get_attribute('src')
# 去除base64编码中的不需要字符
img_base64 = img_base64.replace('%0A', '')
new_img_base64 = img_base64.replace('data:image/png;base64,', '')
# 将图片解码保存
img_file = open('test.png', 'wb')
#print(new_img_base64)
img_file.write(base64.b64decode(new_img_base64))
img_file.close()
# 调用函数获取验证码结果
validate_number = validate_result('test.png')
if validate_number is None:
pass
else:
input_username = driver.find_element_by_id('userName')
input_username.send_keys(id)
input_password = driver.find_element_by_id('password')
input_password.send_keys(password)
input_validate = driver.find_element_by_id(r'captcha')
input_validate.send_keys(validate_number)
login_button = driver.find_element_by_class_name('ant-btn')
login_button.click()
time.sleep(10)
cookie_items = driver.get_cookies()
cookie_str = ""
#组装cookie字符串
for item_cookie in cookie_items:
item_str = item_cookie["name"]+"="+item_cookie["value"]
cookie_str += item_str
#打印出来看一下
return cookie_str
except:
pass
def main(dev_id, start_time, end_time, id, password):
cookies = get_cookie(id, password)
print(cookies)
today = datetime.date.today()
start_list = list(start_time)
end_list = list(end_time)
start_tem = ''.join(start_list[:2] + [':'] + start_list[2:])
end_tem = ''.join(end_list[:2] + [':'] + end_list[2:])
tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
start = tomorrow + ' ' + start_tem
end = tomorrow + ' ' + end_tem
# print(start_time,end_time,start,end)
# 设置开始抢座时间
target_ticks = time.mktime(time.strptime(str(today) + ' 22:29:57', '%Y-%m-%d %H:%M:%S'))
i = 0
while True:
ticks = time.time()
if int(ticks) >= int(target_ticks):
if dev_id + i > 100456281:
print('座位已被抢完')
break
elif dev_id + i <= 100456281 and dev_id + i != 100456087:
yresult = booking(cookies, start, end, start_time, end_time, str(dev_id + i))
if yresult == 1:
print(str(dev_id + i - 100455856) + '号座位预约成功')
break
if yresult == 2:
print(str(dev_id + i - 100455856) + '号座位预约失败')
i = i + 1
continue
else:
print('预约开始时间未到')
# print(cookies)
# wait_times = int(target_ticks) - int(ticks)
# print('预约时间未到,请等待', wait_times, '秒')
# time.sleep(wait_times)
# print(1)
if __name__ == '__main__':
# 五楼的136号座位为100455856+136,五楼共227个座位
dev_id = 100455988
# 每次都需要更改
# 输入明天学习的开始时间和结束时间 当前时间预约冲突
start_time = '0830'
end_time = '2130'
# 输入账号和密码
id = '账户'
password = '密码'
main(dev_id, start_time, end_time, id, password)
|