Today’s New Year’s Eve tickets robbed, circle of friends have joked:

I’ve been single for 20 years and the kirin can’t beat these motherfuckers?

I refreshed the captcha one more second! The tickets are gone?

Get ready for mobike home this year!

Boss, I will not go home this year, I will die in the post!!

I’ve been following this plan for two years now, and every year there’s been no problem, even though that stupid ticketing website is always booming.

The company will have a holiday on February 12th. Please prepare the air tickets on February 11th and train tickets at or in the morning of February 12th.

On January 18, I got up early and opened my computer and mobile phone sleepily. Prepare the software and re-log in to the website on your computer (re-log in again to verify your first ticket purchase

There will only be one code, so I won’t poke fun at the captcha of a ticket website.

Believe in yourself and you can do it in 5 seconds.

Trust yourself, captcha will not go wrong.

I wiped my glasses and the clock stopped at 10:00!

First class: Yes

Second class: Yes

Click to buy

Enter the verification code……

However, I failed to buy tickets because:

There aren’t enough tickets.

I stared at 10:00:06 and couldn’t calm down for a long time…

And then endured the next few minutes when the remaining tickets on the site kept showing up, but couldn’t be bought (actually they weren’t there, they just weren’t refreshed).

I suddenly thought of a friend’s words:

It is now much easier for machines to snatch tickets than for people.

Instead of going to bed, I turned on the computer……

Simulate ticket snatching using Python

– coding: utf-8 –

""" @author: liuyw """
from splinter.browser import Browser
from time import sleep
import traceback
import time, sys
class huoche(object):
	"""docstring for huoche"""
	driver_name=' '
	executable_path=' '
	# username, password
	username = u"[email protected]"
	passwd = u"xxxx"
	# cookies are worth finding on your own. The following two are Respectively Shanghai and Taiyuan South
	starts = u"%u4E0A%u6D77%2CSHH"
	ends = u"%u592A%u539F%2CTYV"
	# time format 2018-01-19
	dtime = u"2018-01-19"
	# number of trains, select the number of trains, 0 from top to bottom click
	order = 0
	# # # passenger name
	users = [u"xxx",u"xxx"]
	# # seats
	xb = u"Second class"
	pz=u"Adult ticket"

	"""Url"""
	ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
	login_url = "https://kyfw.12306.cn/otn/login/init"
	initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306"
	buy="https://kyfw.12306.cn/otn/confirmPassenger/initDc"
	
	def __init__(self):
		self.driver_name='chrome'
		self.executable_path='/usr/local/bin/chromedriver'

	def login(self):
		self.driver.visit(self.login_url)
		self.driver.fill("loginUserDTO.user_name", self.username)
		# sleep(1)
		self.driver.fill("userDTO.password", self.passwd)
		print u"Wait for the verification code and enter it yourself..."
		while True:
			ifself.driver.url ! = self.initmy_url: sleep(1)else:
				break

	def start(self):
		self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path)
		self.driver.driver.set_window_size(1400, 1000)
		self.login()
		# sleep(1)
		self.driver.visit(self.ticket_url)
		try:
			print u"Ticket page open..."
			# sleep(1)
			Load query information
			self.driver.cookies.add({"_jc_save_fromStation": self.starts})
			self.driver.cookies.add({"_jc_save_toStation": self.ends})
			self.driver.cookies.add({"_jc_save_fromDate": self.dtime})

			self.driver.reload()

			count=0
			ifself.order! = 0:while self.driver.url==self.ticket_url:
					self.driver.find_by_text(u"Query").click()
					count += 1
					print u"Loop click query... The % s" % count
					# sleep(1)
					try:
						self.driver.find_by_text(u"Reservation")[self.order - 1].click()
					except Exception as e:
						print e
						print u"We haven't started booking yet."
						continue
			else:
				while self.driver.url == self.ticket_url:
					self.driver.find_by_text(u"Query").click()
					count += 1
					print u"Loop click query... The % s" % count
					# sleep (0.8)
					try:
						for i in self.driver.find_by_text(u"Reservation"):
							i.click()
							sleep(1)
					except Exception as e:
						print e
						print u"Booking %s has not started yet" %count
						continue
			print u"Start booking..."
			# sleep(3)
			# self.driver.reload()
			sleep(1)
			print u'Start selecting users... '
			for user in self.users:
				self.driver.find_by_text(user).last.click()

			print u"Submit order..."
			sleep(1)
			# self.driver.find_by_text(self.pz).click()
			# self.driver.find_by_id('').select(self.pz)
			# # sleep(1)
			# self.driver.find_by_text(self.xb).click()
			# sleep(1)
			self.driver.find_by_id('submitOrder_id').click()
			# print u" Select seat..."
			# self.driver.find_by_id('1D').last.click()
			# self.driver.find_by_id('1F').last.click()Sleep (1.5)print u"Confirm seat selection..."
			self.driver.find_by_id('qr_submit_id'694549689 except Exception as e:print e
			
if __name__ == '__main__':
	huoche=huoche()
	huoche.start()
Copy the code

This code is for reference only, mainly used for communication, prohibited for commercial use.