南京博物院自动化预约

代码

python 复制代码
import time

import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 创建Edge浏览器实例
driver = webdriver.Edge()


def sendMsg(param):
    # 构建消息内容
    message = {
        "msgtype": "text",
        "text": {
            "content": param
        }
    }

    # 替换为您的企业微信Webhook地址
    webhook_url = "**************"

    # 发送消息
    response = requests.post(webhook_url, json=message)

    # 检查响应
    if response.status_code == 200:
        print("消息发送成功")
    else:
        print("消息发送失败")
        print(response.text)


try:
    # 打开登录页面
    driver.get("https://ticket.wisdommuseum.cn/reservation/userOut/outSingle/toSingleIndex.do")
    # driver.get("https://activity.wisdommuseum.cn/reservation/teamNew/outSpecial/toTeamHome.do")

    # 进行登录操作,填写手机号码、密码和验证码等
    # 请根据实际情况修改下面的代码
    telephone = "**********"
    password = "***********"

    telephone_field = driver.find_element(By.ID, "telephone")
    password_field = driver.find_element(By.ID, "password")
    captcha_field = driver.find_element(By.ID, "code")

    telephone_field.send_keys(telephone)
    password_field.send_keys(password)

    # Manually solve captcha
    captcha_text = input("Enter the captcha: ")  # Manually enter the captcha

    captcha_field.send_keys(captcha_text)

    # 登录按钮点击
    login_button = driver.find_element(By.XPATH, "//input[@type='submit']")
    login_button.click()

    time.sleep(5)
    # 切换到iframe
    driver.switch_to.frame("Conframe")

    # 找到指定日期并点击
    target_date = "2024-4-5"
    date_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//td[@lay-ymd='{target_date}']")))


    i=1
    # 检查类是否包含"laydate-disabled",如果是,就刷新页面,直到不再包含这个类
    while "laydate-disabled" in date_element.get_attribute("class"):
        print("第" + str(i) + "次刷新")
        # 等待指定日期元素出现
        driver.get("https://ticket.wisdommuseum.cn/reservation/userOut/outSingle/toSingleIndex.do")
        time.sleep(2)
        # Wait for the frame to be available
        WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "Conframe")))
        date_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//td[@lay-ymd='{target_date}']")))
        # date_element = driver.find_element(By.XPATH, f"//td[@lay-ymd='{target_date}']")
        i=i+1
        # 使用 JavaScript 来删除元素的 class 属性
        # driver.execute_script("arguments[0].removeAttribute('class');", date_element)



    #提交
    date_element.click()

    # Wait for page to load after date selection
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "ss")))

    # checkbox 全部设置为选中状态 <input name="ids" type="checkbox" value="24032973">
    checkboxes = driver.find_elements(By.XPATH, "//input[@type='checkbox']")
    # 遍历并选中所有的 checkbox
    for checkbox in checkboxes:
        checkbox.click()

    driver.find_element(By.ID, "ss").click()

    # 找到确认预约按钮并点击
    confirm_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "layui-layer-btn0")))
    confirm_button.click()

    # 发信息通知我
    # 构建消息内容

    time.sleep(10)
    sendMsg("预约成功")
except Exception as e:
    print(e)

finally:
    # 关闭浏览器
    driver.quit()

注意

需要先去官网录如下预约人信息 验证码也要输一次 然后挂到后台 仅供学习使用 请勿进行非法行为

相关推荐
不写八个7 分钟前
Python办公自动化教程(005):Word添加段落
开发语言·python·word
_.Switch24 分钟前
Python机器学习框架介绍和入门案例:Scikit-learn、TensorFlow与Keras、PyTorch
python·机器学习·架构·tensorflow·keras·scikit-learn
赵荏苒36 分钟前
Python小白之Pandas1
开发语言·python
一眼万里*e1 小时前
fish-speech语音大模型本地部署
python·flask·大模型
结衣结衣.2 小时前
python中的函数介绍
java·c语言·开发语言·前端·笔记·python·学习
茫茫人海一粒沙2 小时前
Python 代码编写规范
开发语言·python
林浩2332 小时前
Python——异常处理机制
python
数据分析螺丝钉2 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
小蜗笔记3 小时前
在Python中实现多目标优化问题(7)模拟退火算法的调用
开发语言·python·模拟退火算法
TANGLONG2223 小时前
【C语言】数据在内存中的存储(万字解析)
java·c语言·c++·python·考研·面试·蓝桥杯