Automate 12306 Train Ticket Booking with Python and Splinter

This tutorial explains how to use Python 2.7 and the Splinter web‑automation library to log into 12306, search for tickets, handle cookies, and automatically complete the booking steps, requiring only manual captcha entry and payment confirmation.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate 12306 Train Ticket Booking with Python and Splinter

Want to grab a train ticket from 12306 automatically? This guide shows how to use Python 2.7 with the Splinter library to script the login, search, and booking process.

Tools needed:

12306 account credentials

Python 2.7

Chrome browser and chromedriver

The script from https://github.com/ppy2790/tickets

Splinter is an open‑source web‑application testing tool that drives a real browser, allowing you to automate interactions with the site.

How it works

The script opens the relevant 12306 URLs, locates the form controls for login, search and ticket confirmation, and fills them automatically. The most difficult part is extracting the hidden “start station” value from cookies.

Query URL: https://kyfw.12306.cn/otn/leftTicket/init

Login URL: https://kyfw.12306.cn/otn/login/init

My 12306 URL: https://kyfw.12306.cn/otn/index/initMy12306

Confirm URL: https://kyfw.12306.cn/otn/confirmPassenger/initDc

The script reads the cookie to obtain the start‑station code, stores common city codes in a dictionary, and then performs the ticket search.

Running the script performs all steps automatically; you only need to solve the 12306 captcha manually and confirm payment after a ticket is found.

Images illustrate the UI, control names, cookie values, and the successful booking screen.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Web Automationticket booking12306splinter
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.