问题遇到的现象和发生背景
学校微信端预约程序,手动抢根本抢不到,12点开网站,12:00:03全部被预约,于是想通过python实现快速自动化预约。
问题相关代码,请勿粘贴截图
```python
from selenium.webdriver.common.by import By
import json
import os
import time
from selenium.common.exceptions import NoSuchElementException
import time
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
import time
s = Service(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.implicitly_wait(3)
driver.get('http://gym.ujs.edu.cn/index.php/index/yuyue/list.html?item_id=3')#打开预约网址
driver.delete_all_cookies()
time.sleep(1)
向浏览器添加保存的cookies
cookies = json.load(open("cookies.txt", "rb"))
for cookie in cookies:
cookie_dict = {
"domain": cookie.get('domain'),
'name': cookie.get('name'),
'value': cookie.get('value'),
"expires": "",
'path': '/',
'httpOnly': True,
'HostOnly': False,
'Secure': False}
driver.add_cookie(cookie_dict)
driver.refresh()
button =driver.find_elements_by_class_name('weui-btn weui-btn_primary')
driver.find_element_by_class_name('weui-btn weui-btn_primary').click()
driver.find_element(by=By.LINK_TEXT,value='登录').click()
time.sleep(0.5)
driver.find_element_by_class_name('office-time-item slot_').click()
driver.find_element(by=By.XPATH,value="//div[@class='office-time-item slot_' and @data-id='3']").click()
driver.find_element(by=By.LINK_TEXT,value='选择场地').click()
driver.find_element(by=By.XPATH,value="//div[@class='office-time-item area_' and @area-id='17']").click()
time.sleep(0.5)
#测试代码
driver.find_element(by=By.XPATH,value="//div[@class='office-time-item timeslot_ disable' and @data-id='6856']").click()
#实际运行代码
driver.find_element(by=By.XPATH,value="//div[@class='office-time-item timeslot_ able' and @data-id='6856']").click()
driver.find_element(by=By.LINK_TEXT,value='立即支付').click()
```python
import pyautogui
import time
FAILSAFE = False
xy = pyautogui.locateOnScreen('sport.png', confidence=0.7) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
def actionA():
# xy = pyautogui.locateOnScreen('sport.png',confidence=0.7) #
# center = pyautogui.center(xy) # 寻找图片的中心
# pyautogui.click(center)
pyautogui.scroll(-400)
time.sleep(1)
pyautogui.scroll(-400)
xy = pyautogui.locateOnScreen('load.png',confidence=0.7) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
time.sleep(0.5)
xy = pyautogui.locateOnScreen('back.png', confidence=0.7) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
pyautogui.moveRel(0, 50, duration=0.25)
pyautogui.scroll(-400)
def actionB():
pyautogui.scroll(-400)
time.sleep(1)
xy = pyautogui.locateOnScreen('yumaoqiu.png',confidence=0.7) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
pyautogui.scroll(-400)
time.sleep(1)
xy = pyautogui.locateOnScreen('choose.png',confidence=0.7) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
time.sleep(1)
pyautogui.scroll(-800)
xy = pyautogui.locateOnScreen('sujiao.png',grayscale=True,confidence=0.8) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
pyautogui.scroll(-400)
xy = pyautogui.locateOnScreen('eight.png',confidence=0.7) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
pyautogui.scroll(-400)
xy = pyautogui.locateOnScreen('zhifu.png',confidence=0.9) #
center = pyautogui.center(xy) # 寻找图片的中心
pyautogui.click(center)
while True:
if pyautogui.locateOnScreen('yumaoqiu.png',confidence=0.7):
actionB()
print("预约成功")
else:
pyautogui.scroll(-400)
actionA()
print("执行预约")
pyautogui.scroll(-400)
运行结果及报错内容
思路一:selenium自动化,结果支付端必须要求在微信端上,失败
思路二:利用pyautogui自动识图点击模拟微信端,速度适中,置信度设置0.7还能将就,但是由于网站12点才开启,代码编写过程中发现逻辑有问题,会一直执行else下的刷新而不执行函数B下的预约程序。暂时搁置
我的解答思路和尝试过的方法
方法如思路一与思路二
我想要达到的结果
想要实现微信端自动化,还能有其他精确快速的方法吗?或者思路二的逻辑怎么样修改?