南京博物院自动化预约

代码

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()

注意

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

相关推荐
Bellafu6663 小时前
selenium常用的等待有哪些?
python·selenium·测试工具
小白学大数据4 小时前
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
爬虫·python·ajax
2401_841495645 小时前
【计算机视觉】基于复杂环境下的车牌识别
人工智能·python·算法·计算机视觉·去噪·车牌识别·字符识别
Adorable老犀牛5 小时前
阿里云-ECS实例信息统计并发送统计报告到企业微信
python·阿里云·云计算·企业微信
倔强青铜三5 小时前
苦练Python第66天:文件操作终极武器!shutil模块完全指南
人工智能·python·面试
倔强青铜三5 小时前
苦练Python第65天:CPU密集型任务救星!多进程multiprocessing模块实战解析,攻破GIL限制!
人工智能·python·面试
Panda__Panda5 小时前
docker项目打包演示项目(数字排序服务)
运维·javascript·python·docker·容器·c#
Lris-KK6 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
zy_destiny7 小时前
【工业场景】用YOLOv8实现抽烟识别
人工智能·python·算法·yolo·机器学习·计算机视觉·目标跟踪
(●—●)橘子……7 小时前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode