注:本脚本仅供学习和研究使用,请勿用于违法行为。

  1. 安装必要的Python库:requests、selenium、webdriver_manager
pip install requests
pip install selenium
pip install webdriver_manager
  1. 下载Chrome浏览器对应版本的Driver,并解压到任意目录下。例如:
# Chrome 95版本的Driver下载地址
https://chromedriver.storage.googleapis.com/95.0.4638.54/chromedriver_win32.zip

# 解压到C盘根目录下
C:\chromedriver_win32\chromedriver.exe
  1. 编写脚本
import time
import requests
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

# 登录信息
username = 'your_username'
password = 'your_password'

# 活动信息
activity_id = 'activity_id'
seat_type_id = 'seat_type_id'

# 验证码接口
captcha_url = 'https://captcha.damai.cn/api/image'

# 登录接口
login_url = 'https://passport.damai.cn/login'

# 提交订单接口
order_url = 'https://buy.damai.cn/orderConfirm'

# 请求头信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
    'Referer': 'https://www.damai.cn/',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
}

# 创建浏览器实例
driver = webdriver.Chrome(ChromeDriverManager().install())

# 打开登录页面并输入账号密码
driver.get(login_url)
driver.find_element_by_id('login_email').send_keys(username)
driver.find_element_by_id('login_pwd').send_keys(password)
driver.find_element_by_id('login_submit').click()

# 等待登录成功并跳转到活动页面
while True:
    if driver.current_url.startswith('https://www.damai.cn/'):
        break
    time.sleep(1)

# 循环请求验证码,直到成功为止
while True:
    response = requests.get(captcha_url, headers=headers)
    if response.status_code == 200:
        with open('captcha.png', 'wb') as f:
            f.write(response.content)
        break
    time.sleep(1)

# 手动输入验证码
captcha_code = input('请输入验证码:')

# 跳转到活动页面
driver.get(f'https://detail.damai.cn/item.htm?id={activity_id}')

# 选择场次和座位类型
driver.find_element_by_id(f'seat_{seat_type_id}').click()
driver.find_element_by_id('btnBuyNow').click()

# 等待页面跳转
while True:
    if driver.current_url.startswith('https://buy.damai.cn/'):
        break
    time.sleep(1)

# 填写验证码并提交订单
driver.find_element_by_id('captcha').send_keys(captcha_code)
driver.find_element_by_id('orderConfirmSubmit').click()

# 等待订单提交结果
while True:
    if driver.current_url.startswith(order_url):
        print('抢票成功!')
        break
    elif driver.current_url.startswith('https://www.damai.cn/'):
        print('抢票失败!')
        break
    time.sleep(1)

# 关闭浏览器
driver.quit()
  1. 修改脚本中的登录信息、活动信息等参数

其中:

  • username:大麦网账号
  • password:大麦网密码
  • activity_id:活动ID,可以在活动详情页面URL中找到
  • seat_type_id:座位类型ID,可以在购买页面HTML中找到
  1. 运行脚本

在命令行中进入脚本所在目录,执行以下命令:

python script.py
  1. 手动输入验证码

运行脚本后,会自动打开登录页面,登录成功后会跳转到活动页面。此时需要手动输入验证码,脚本会自动获取验证码图片并保存到当前目录下的captcha.png文件中。输入完成后按回车键即可。

  1. 等待抢票结果

输入验证码后,脚本会自动选择场次和座位类型,并填写验证码。提交订单后,脚本会等待页面跳转,最终输出抢票结果。如果抢票成功,可以在订单页面中查看订单信息。

注:如果抢票失败,可以尝试多次运行脚本,或者调整座位类型等参数。同时,为了提高抢票成功率,建议在活动开售前就启动脚本,以确保能够及时提交订单

给我写一段大麦网自动抢票的python脚本并教我怎么用

原文地址: https://www.cveoy.top/t/topic/gDH8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录