基于python的大麦网自动抢票工具实现代码
时间: 2024-01-18 12:04:17 浏览: 53
抢票工具的实现需要用到自动化测试工具Selenium和浏览器驱动ChromeDriver。以下是基于Python的大麦网自动抢票工具实现代码:
```python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
# 抢票链接、账号密码以及票数
url = 'https://detail.damai.cn/item.htm?spm=a2oeg.search_category.0.0.2f4b4f80pLNEgW&id=XXXXX' # 抢票链接
username = 'XXXXX' # 大麦网账号
password = 'XXXXX' # 大麦网密码
ticket_num = 1 # 抢票数量
# Chrome浏览器设置
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument('--disable-infobars')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-default-apps')
options.add_argument('--disable-plugins')
options.add_argument('--incognito')
options.add_argument('--start-maximized')
options.add_argument('--disable-translate')
options.add_argument('--disable-file-system')
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36')
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 10)
# 打开网页并登录
driver.get(url)
driver.maximize_window()
time.sleep(1)
login = wait.until(EC.presence_of_element_located((By.XPATH, '//a[@class="btn btn-blue"]')))
ActionChains(driver).move_to_element(login).click().perform()
username_input = driver.find_element_by_xpath('//input[@id="login_email"]')
password_input = driver.find_element_by_xpath('//input[@id="login_password"]')
username_input.send_keys(username)
password_input.send_keys(password)
time.sleep(1)
login_button = driver.find_element_by_xpath('//a[@class="btn-login"]')
ActionChains(driver).move_to_element(login_button).click().perform()
# 抢票
while True:
try:
buy_button = wait.until(EC.presence_of_element_located((By.XPATH, '//a[@class="buybtn"]')))
if buy_button.text == '即将开抢':
print('抢票未开始,等待中...')
time.sleep(1)
elif buy_button.text == '立即预订':
print('抢票已开始,开始尝试预订...')
buy_button.click()
break
except:
pass
# 选择票数
if ticket_num > 1:
select_ticket_button = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="cafe-c-input-number-handler cafe-c-input-number-handler-up"]')))
for i in range(ticket_num-1):
select_ticket_button.click()
# 提交订单
submit_button = wait.until(EC.presence_of_element_located((By.XPATH, '//a[@class="cafe-c-button cafe-c-button--lred J_Go"]')))
ActionChains(driver).move_to_element(submit_button).click().perform()
time.sleep(3)
confirm_button = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="mod-common-dialog J_Mod"]/div[@class="content"]/a')))
ActionChains(driver).move_to_element(confirm_button).click().perform()
print('抢票成功!')
```
其中, XXXXX 需要根据实际情况进行替换。使用时将代码复制到Python文件中,在命令行中运行即可。