-
-
Save JiQingzhe/75700337797914f4310c94392712e3c7 to your computer and use it in GitHub Desktop.
抢高铁票
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import random | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.ui import WebDriverWait | |
# 设置等待超时时间和轮询间隔时间 | |
WAIT_TIMEOUT = 10 | |
POLL_FREQUENCY = 2 | |
# 定义并初始化 driver | |
options = webdriver.EdgeOptions() | |
options.use_chromium = True | |
options.add_experimental_option('excludeSwitches', ['enable-automation']) | |
options.add_experimental_option('useAutomationExtension', False) | |
driver = webdriver.Edge(options=options) | |
# 车次、出发日期和出发时间 | |
train_number = 'G' | |
start_date = '2023-' | |
start_time = '00:00' | |
# 乘客信息 | |
passenger_name = 'name' | |
id_card_number = 'ID#身份证号' | |
# 出发站和到达站 | |
from_station = 'XXX'#换成出发站 | |
to_station = 'XXX'#换成到达站 | |
# 座位类型和对应的坐席类型编号 | |
seat_type = '二等座' | |
seat_type_dict = { | |
'商务座': '9', | |
'一等座': 'M', | |
'二等座': 'O', | |
'特等座': 'P', | |
'软卧': '4', | |
'硬卧': '3', | |
'软座': '2', | |
'硬座': '1', | |
'无座': '1' | |
} | |
# 定时抢票时间 | |
book_date = '2023-0' | |
book_time = '13:59:50'#一般12306是在下午两点开售,所以提前十秒 | |
while True: | |
now = datetime.datetime.now() | |
if str(now.date()) == book_date and now.time() >= datetime.datetime.strptime(book_time, '%H:%M:%S').time(): | |
break | |
time.sleep(1) | |
driver.refresh() | |
# 登录 12306 账户 | |
def login(driver): | |
# 打开登录页面 | |
driver.get('https://kyfw.12306.cn/otn/resources/login.html') | |
# 等待登录按钮出现并点击 | |
login_button = WebDriverWait(driver, WAIT_TIMEOUT, POLL_FREQUENCY).until( | |
EC.presence_of_element_located((By.XPATH, "//a[@class='login-hd-account']"))) | |
login_button.click() | |
# 随机等待一段时间(1-3 秒)模拟人类操作 | |
time.sleep(random.uniform(1, 3)) | |
# 输入账号和密码并点击登录 | |
account_input = WebDriverWait(driver, WAIT_TIMEOUT, POLL_FREQUENCY).until( | |
EC.presence_of_element_located((By.XPATH, "//input[@id='J-userName']"))) | |
account_input.clear() | |
account_input.send_keys('XXXX') # 替换成自己的账号 | |
password_input = driver.find_element_by_xpath("//input[@id='J-password']") | |
password_input.clear() | |
password_input.send_keys('XXXX') # 替换成自己的密码 | |
login_submit_button = driver.find_element_by_xpath("//a[@id='J-login']") | |
login_submit_button.click() | |
# 随机等待一段时间(1-3 秒)模拟人类操作 | |
time.sleep(random.uniform(1, 3)) | |
print('登录成功') | |
# 填写订单信息 | |
def fill_order_info(driver): | |
# 等待联系人信息页面出现 | |
WebDriverWait(driver, WAIT_TIMEOUT, POLL_FREQUENCY).until( | |
EC.presence_of_element_located((By.XPATH, "//span[@id='dialog_xsertcj_ok']"))) | |
# 关闭弹出的订票须知窗口 | |
close_notice_button = driver.find_element_by_xpath("//div[@id='lay_xsertcj']/div/a") | |
close_notice_button.click() | |
# 随机等待一段时间(1-3 秒)模拟人类操作 | |
time.sleep(random.uniform(1, 3)) | |
# 填写乘客信息 | |
passenger_name_input = driver.find_element_by_xpath("//input[@name='passenger_0_name']") | |
passenger_name_input.clear() | |
passenger_name_input.send_keys(passenger_name) | |
id_card_number_input = driver.find_element_by_xpath("//input[@name='passenger_0_cardno']") | |
id_card_number_input.clear() | |
id_card_number_input.send_keys(id_card_number) | |
# 选择座位类型 | |
seat_type_select = driver.find_element_by_xpath("//select[@id='seatType_1']") | |
seat_type_option = seat_type_select.find_element_by_xpath(f"//option[@value='{seat_type_dict[seat_type]}']") | |
seat_type_option.click() | |
# 提交订单 | |
submit_order_button = driver.find_element_by_xpath("//a[@id='submitOrder_id']") | |
submit_order_button.click() | |
# 等待确认订单窗口出现 | |
WebDriverWait(driver, WAIT_TIMEOUT, POLL_FREQUENCY).until( | |
EC.presence_of_element_located((By.XPATH, "//div[@id='qr_submit_id']/a"))) | |
# 提交确认订单 | |
confirm_submit_button = driver.find_element_by_xpath("//div[@id='qr_submit_id']/a") | |
confirm_submit_button.click() | |
print('订单提交成功!') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment