Home > System Tutorial > LINUX > Use Python code to automatically grab train tickets

Use Python code to automatically grab train tickets

WBOY
Release: 2024-04-25 08:25:29
forward
405 people have browsed it

用 Python 代码自动抢火车票

Everyone should be very familiar with many train ticket grabbing software on the market, but few people have studied how to implement it, so they think it is mysterious, but it is actually very simple. The following uses Python to simulate the ticket grabbing program to reveal to you what the ticket grabbing process is.

This code is for reference only and is mainly used for communication. Commercial use is prohibited.

The specific code is as follows, you can modify it to your own 12306 username account:

用 Python 代码自动抢火车票

Preparation Tools
  • 12306Website username and password
  • Python
  • chrome browser and download chromedriver
  • Download the Python code, located at: https://github.com/ppy2790/tickets

The code is developed using Python Splinter. Splinter is an open source web application testing tool developed using Python. It can help you automatically browse and interact with the site. Splinter official website

http://splinter.readthedocs.io/en/latest/. When Splinter is executed, it will automatically open the browser you specify and access the specified URL. Then any behavior of the simulation you develop will be completed automatically. You only need to sit in front of the computer, watch the various actions on the screen automatically complete and collect the results like watching a movie.

Understand the principle

Find the corresponding URL and find the control to simulate login, query, and ticket booking operations. The key is to find the name of the control. The difficulty is that the starting point is not the page value entered directly, and it needs to be found in the cookie.

  • 12306 Query URL: https://kyfw.12306.cn/otn/leftTicket/init
  • 12306 Login URL: https://kyfw.12306.cn/otn/login/init
  • My 12306URL: https://kyfw.12306.cn/otn/index/initMy12306
  • Ticket purchase confirmation URL: https://kyfw.12306.cn/otn/confirmPassenger/initDc

Python code opens the URL and finds the control fill value:

用 Python 代码自动抢火车票

用 Python 代码自动抢火车票

Find the username and password control name

用 Python 代码自动抢火车票

Find the starting control name

To determine the value of the starting location, use the "Check" function in the Chrome browser (press F12) and find it in Network ---> Cookies:

用 Python 代码自动抢火车票

The value of the starting location in the cookie

Copy the cookie value of the starting place. I copied several commonly used cities and put them in the dictionary:

用 Python 代码自动抢火车票

Query ticket code:

用 Python 代码自动抢火车票

Actually, you just need to run the code:

用 Python 代码自动抢火车票

Of course, what still needs to be manually clicked is the evil 12306 verification code. Just confirm the payment after grabbing the ticket.

用 Python 代码自动抢火车票

Ticket grabbing is in progress

用 Python 代码自动抢火车票

Success in grabbing tickets!

The above is the detailed content of Use Python code to automatically grab train tickets. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template