没有Python编程基础的人看这一篇:抢票软件,不用自己运行脚本了(适应新版网站)
12306官网更新后下方代码有较多bug,不建议使用下方代码,建议使用上方新版软件(自适应网站更新(无论网站如何更新,软件自适应))
一个清明假期,差点把我困在家里面出不来,没有车票,好在今天博主写了一个抢票脚本,现场演示:
完整链接在下方:
导入必要的库
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time
反爬虫处理
script = 'Object.defineProperty(navigator,"webdriver",{get:()=>undefined,});'
driver.execute_script(script)
用户登录
扫码登录
输入出发地、目的地和出发日期
from_station_input = driver.find_element(By.ID, "fromStationText")
from_station_input.send_keys(here_place)
to_station_input = driver.find_element(By.ID, "toStationText")
to_station_input.send_keys(to_place)
date_input = driver.find_element(By.ID, "train_date")
date_input.send_keys(travel_date)
根据车次情况预定车票
def order_ticket():
# 输入想乘坐的火车车次
train_num = input('---输入你想乘坐的火车车次---\n')
train_numbers = driver.find_elements(By.XPATH, '//tbody[@id="queryLeftTable"]/tr[not(@datatran)]')
# 遍历车次信息,寻找目标车次
for train_number in train_numbers:
train_messages = train_number.text.replace('\n', ' ')
train_message = train_messages.split(' ')
if train_num == train_message[0]:
if '有' == train_message[10] or '有' == train_message[9]:
button = train_number.find_element(By.XPATH, './/a[@class="btn72"]')
button.click()
print('预定成功,现在正在选择乘车人')
break
else:
print('正在为你刷新页面,实时更新车票情况')
# 调用刷新页面函数
refresh_search_ticket(train_message, train_number)
break
确认乘车人和座位信息 默认选择第一个人(可以自己修改)
# 点击乘车人信息,默认第一个为本人
driver.find_element(By.ID, 'normal_passenger_id').click()
# 点击确认按钮
confirm_button = driver.find_element(By.ID, 'dialog_xsertcj_ok')
confirm_button.click()
# 选择座位
for seat, xpath in seats.items():
driver.find_element(By.XPATH, xpath).click()
print(f"已点击座位 {seat}")
confirm_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "qr_submit_id"))
)
confirm_button.click()
print(f"已点击确认按钮")
© 版权声明
THE END
暂无评论内容