Python 基于 Selenium 实现 12306 自动购票脚本
前言
本教程演示如何利用 Python 的 Selenium 自动化测试模块模拟人类操作浏览器,从而实现对 12306 网站的车票查询与预订功能。通过编写脚本,可以自动化完成登录、查票、选座及提交订单等步骤。
环境准备
在运行脚本前,请确保已完成以下准备工作:
- Python 环境:安装 Python 3.x 版本。
- 依赖库:使用 pip 安装 Selenium 库。
pip install selenium - 浏览器驱动:下载与本地 Chrome 浏览器版本一致的 chromedriver.exe。将驱动文件放置于脚本同一目录下,或将其添加到系统环境变量 PATH 中。
- 浏览器设置:建议使用 Chrome 浏览器,并关闭自动更新弹窗干扰。
核心代码实现
以下是完整的抢票脚本结构,包含初始化、登录、搜索、下单等核心方法。
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Qiangpiao():
def __init__(self, from_station, to_station, depart_time, train_num, passenger):
self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'
self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html'
self.order_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
self.from_station = from_station
self.to_station = to_station
.depart_time = depart_time
.train_num = train_num
.passenger = passenger
.now_month = datetime.date.today().month
.leave_month = (.depart_time.split()[])
.leave_day = (.depart_time.split()[])
.driver = webdriver.Chrome()
():
.driver.get(.login_url)
.driver.set_window_size(, )
WebDriverWait(.driver, ).until(EC.url_to_be(.init_my_url))
()
():
time.sleep()
:
.driver.find_element(By.XPATH, ).click()
Exception:
():
action = ActionChains(.driver)
element = .driver.find_element(By.LINK_TEXT, )
action.move_to_element(element).perform()
.driver.find_element(By.XPATH, ).click()
.driver.find_element(By.LINK_TEXT, ).click()
():
.driver.find_element(By.ID, ).click()
.driver.find_element(By.ID, ).send_keys(.from_station)
.driver.find_element(By.ID, ).send_keys(Keys.ENTER)
.driver.find_element(By.ID, ).click()
.driver.find_element(By.ID, ).send_keys(.to_station)
.driver.find_element(By.ID, ).send_keys(Keys.ENTER)
.driver.find_element(By.ID, ).click()
.leave_month == .now_month:
xpath_str =
.leave_month == .now_month + :
xpath_str =
:
()
EC.element_to_be_clickable((By.XPATH, xpath_str)):
.driver.find_element(By.XPATH, xpath_str).click()
:
()
WebDriverWait(.driver, ).until(EC.element_to_be_clickable((By.ID, )))
search_btn = .driver.find_element(By.ID, )
search_btn.click()
WebDriverWait(.driver, ).until(EC.presence_of_element_located((By.XPATH, )))
():
train_num_list = []
train_num_ele_list = .driver.find_elements(By.XPATH, )
t train_num_ele_list:
train_num_list.append(t.text)
tr_list = .driver.find_elements(By.XPATH, )
.train_num train_num_list:
tr tr_list:
train_num = tr.find_element(By.XPATH, ).text
.train_num == train_num:
text_1 = tr.find_element(By.XPATH, ).text
text_2 = tr.find_element(By.XPATH, ).text
(text_1 == text_1.isdigit()) (text_2 == text_2.isdigit()):
order_btn = tr.find_element(By.CLASS_NAME, )
order_btn.click()
WebDriverWait(.driver, ).until(EC.url_to_be(.order_url))
.driver.find_element(By.XPATH, ).click()
EC.presence_of_element_located((By.XPATH, )):
.driver.find_element(By.ID, ).click()
.driver.find_element(By.ID, ).click()
time.sleep()
.driver.find_element(By.ID, ).click()
()
:
()
:
()
():
._login()
._pop_window()
._enter_order_ticket()
._search_ticket()
._order_ticket()
time.sleep()
.driver.quit()
__name__ == :
qiangpiao = Qiangpiao(, , , , )
qiangpiao.run()


