直接上代码
这个是预约羽毛球场的代码
代码不会报错,但是在最后预约的时候不能成功,不知道哪里有问题,请大家参照代码帮忙解答
import requests
import time
import json
from lxml import etree
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import sys
import datetime
from selenium import webdriver
def Login(name, psw):
url = 'http://cgzx.tju.edu.cn:8080/index.php/Book/Login/authCheck.html'
url_site='http://cgzx.tju.edu.cn:8080/index.php/Book/Book/index3.html?day=2020-08-09&time=00002&cg=01&cp=02'
index4_url="http://cgzx.tju.edu.cn:8080/index.php/Book/Book/index4"
order_url ='http://cgzx.tju.edu.cn:8080/index.php/Book/Book/order.html'
index5_url = 'http://cgzx.tju.edu.cn:8080/index.php/Book/Book/index5.html'
head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
}
# 登陆参数
param = {'name': name, 'pwd': psw}
session=requests.Session()
login_ok= session.post(url, headers=head, data=param)
day = time.strftime('%Y-%m-%d', time.localtime(time.time() + 24 * 60 * 60*2 ))
print( day)
page_site = session.get(url_site,headers=head).text
# page_site对实例化操作
tree=etree.HTML(page_site)
site_list=tree.xpath('//div[@id="spaceList"]/div')
print(site_list)
for div in site_list:
name = div.xpath('./@name')[0]
print(name)
# 变量拼接
param='?day='+str(day)+'&time='+'00002'+'&cg=01&cp=02&cdinfoid='+str(name)
print(index4_url+param)
page_site_rqs = session.get(index4_url+param, headers=head).text
# 对page_site_rqs实例化操作
tree_order = etree.HTML(page_site_rqs)
SEQ_NO = tree_order.xpath('//td[@class="tl vt"]/input[6]/@value')[0]
hash = tree_order.xpath('//div[@class ="navigate box"]/form/input/@value')[0]
print('SEQ_NO',SEQ_NO,hash)
# 准备变量<td class="tl vt">
data = {
'__hash__': hash,
'CELL_PHONE': ' ####',
'REAL_NAME': ' ###',
'CGINFO_ID': ' 03',
'CDINFO_ID': name,
'CAMPUS_ID': ' 02',
'SEQ_NO': SEQ_NO,
'PRICE': ' 0',
'DISCOUNT': ' 1',
'PRICE_FINAL': ' 0'
}
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
'Location': 'http://cgzx.tju.edu.cn:8080/index.php/Book/Book/index5.html'
}
# real_url=session.post(order_url,data=data, headers=head)
# print(real_url.status_code)
# print(real_url.url)
page_site = session.post(url=order_url, data=data, headers=head)
# 判断请求是否成功
print(page_site.status_code)
# print(page_site )
# # 进行登录后的访问
# res = session.post(url, data=data, headers=head, verify=False)
# content = res.text
# opt = webdriver.ChromeOptions()
# driver = webdriver.Chrome(options=opt)
# driver.get('http://cgzx.tju.edu.cn:8080/index.php/Book/Book/index4.html?day=2020-08-07&time=00002&cg=01&cp=02&cdinfoid=2204')
# driver.find_element_by_xpath("//span[@class='middle blue_bg']/input").click() # 点击按钮
# print(content)
return (session.cookies.get_dict())
def Send(flag, mess): # 传入flag(成功与否的标志),mess(预定的位置时间)
mail = smtplib.SMTP()
mail.connect("smtp.qq.com") # 连接 qq 邮箱的SMTP服务
mail.login("x1@qq.com", "xxxx") # 账号和授权码
if flag == 1:
print('预定成功')
message = MIMEText(mess) # 邮件内容
message['Subject'] = Header('羽毛球场预定成功提醒') # 邮件主题
mail.sendmail("x1@qq.com", ["x2@qq.com"], message.as_string()) # 发送账号、接收账号和邮件信息
if __name__ == '__main__':
print('sleep_now', end='') # 未到时间,休眠
# while True:
strtime = time.strftime('%H:%M:%S', time.localtime(time.time()))
print(strtime)
name=###
psw=###
# if strtime == '.12:00:00':
# 返回一个dict
jsid=Login(name, psw)
# print(jsid)
# print('Start Booking')
# res = Res_login(jsid, '36')
# Res_book(jsid, '36')
# trytime = 1
# while res.headers['Content-Length'] != '1':
# trytime += 1
# res = Res_book(jsid, '36')
# time.sleep(6)
# if trytime > 20:
# # Send(0, '预定失败')
# print(trytime)
# sys.exit()
# Send(1, '预定成功')
# break
# # else:
# # time.sleep(10)
# # print('.', end='')