程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Python to grab seats in the library (automatic reservation)

編輯:Python

Script function

When the system opens the seat, it can quickly reserve the designated position

You can set the appointment period

After running, I will always help you grab , Need to stop manually

Even if you encounter a stronger script, it will automatically help you grab a seat

Realization

First, solve the login problem , adopt F12 Find the login request package , Through analysis and comparison of packages, it can be found that generally only the user name and password parameters are changed , And then use requests.session() To request the login interface , Login successful .

Then make a reservation and grab your bag , Analyze the package to find the key parameters of the change , Generally, the only key parameter is the seat id、 Starting time 、 End time , Just copy the other parameters , Next, use the one that just logged in requests.session() Instance to request .

Optimize

Since you help rob every day, you can't keep asking , One is to burden the server , Second, if there is any anti climbing strategy, it is easy to be found and blocked . So every day 12 spot ( The system reserves the opening hours ) Just send the reservation package when you get there .

What if the script crashes and fails to grab the seat set by itself , Return the result of the appointment , I found that someone had reserved the next seat automatically , You can also write your favorite seat on the list , You can't grab the next one .

Code

( In order not to burden the school system , Code for reference only , Can't run directly )

import requests
from datetime import date
from datetime import timedelta
import json
import getopt
import sys
import time
global headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',
'Host': '',
}
# Login function
def login(id, pwd):
l_params = {
'id': id,
'pwd': pwd,
'act': 'login',
}
# The interface obtained through self analysis , The same below
login_url = ''
s = requests.session()
re = s.post(url=login_url, headers=headers, params=l_params)
# Return the instance of successful login
return s
# Reservation function
def yd(s, start, end, s_id):
y_params = {
"dialogid": "",
# Reserve a seat
"dev_id": s_id,
"room_id":"",
"type":"dev",
"prop":"",
"number":"",
"classkind":"",
"test_name":"",
"start": start,
"end": end,
"start_time": "800",
"end_time": "2200",
}
y_url = ''
re = s.get(url=y_url, headers=headers, params=y_params)
print(re.text)
res = json.loads(re.text)
msg = res['msg']
if msg == ' Successful operation !':
print('success')
return 1
elif msg == '2020-09-11 Only in advance [1] Day appointment ':
return 0
else:
print('fail')
return 2
# Script input prompt
def useage():
print(
'''
Usage:
-i Student number ( Required )
-p password
-s seat id
-b Starting time , Format 13:00, The same below
-e End time
''')
if __name__ == '__main__':
id = pwd = None
# Do not fill in the default to grab this seat in this time period
s_id = '100458282'
begin = '8:00'
end = '22:00'
# Process input
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:p:s:b:e:')
for name, value in opts:
if name == '-i':
id = value
if name == '-p':
pwd = value
if name == '-s':
s_id = value
if name == '-b':
begin = value
if name == '-e':
end = value
except getopt.GetoptError:
useage()
if not id:
useage()
sys.exit(3)
if not pwd:
pwd = id
# First get the day after tomorrow
aftertomorrow = date.isoformat(date.today() + timedelta(days=2))
while True:
hour = int(time.strftime('%H',time.localtime(time.time())))
m = int(time.strftime('%M', time.localtime(time.time())))
# If the present tomorrow waits for the day after tomorrow , Here we are 12 spot , Start grabbing seats
if date.isoformat(date.today() + timedelta(days=1)) == aftertomorrow:
s = login(id, pwd)
start = aftertomorrow + ' ' + begin
endtime = aftertomorrow + ' ' + end
result = yd(s, start, endtime, s_id)
if result == 1:
# The appointment was successful
aftertomorrow = date.isoformat(date.today() + timedelta(days=2))
sleep_time = (23 - hour) * 3600 + (59 - m) * 60 + 35
print(' Program sleep {}s'.format(sleep_time))
print(aftertomorrow)
time.sleep(sleep_time)
elif result == 2:
# Be booked , Grab one id A seat for
s_id = str(int(s_id) - 1)
continue
else:
continue
else:
sleep_time = (23 - hour) * 3600 + (59 - m) * 60
time.sleep(sleep_time)

Conclusion

I wonder what kind of reservation system your school library has , But most schools don't spend too much money on this , So the system is garbage , Most of them can follow this idea . And some schools use almost the same system as ours , It must be from the same company or the same template . If your school has a similar system , Code changes can be used .

After reading it, I want to do it myself, but I don't know how to analyze it ? Look at this

For Xiaobai who just learned to crawl , After reading, I can understand

My school's system homepage looks like this


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved