Table of contents
2.1. Click to purchase tickets
2.2. Enter the train information in the search box
1. Description
1.1. Background
Continuing from the above, I couldn’t get tickets for the Spring Festival travel rush. I tried to solve this problem through code. The following content is the second part.
1.2. Description
Operating system: win 10
Editor:pycharmedu
Language and version: python 3.10
Library used: selenium
Implementation idea: Use selenium to simulate human behavior and perform automated operations
About the code: The complete code will be released in the third part to fully realize the function.
The first part of the content-->The login problem has been solved.
Browser download and driver installation: Firefox browser download and driver installation
Part One content link: Part One
Browser crash explanation: It is most likely because the downloaded browser and browser driver versions are inconsistent. It is recommended to use a search engine to find the content shared by the boss to solve the problem.
The following URL will not be the real one, use it for testing, pay attention to identify it and change it yourself.
The automated ticket purchase flow chart function is implemented as shown in Figure 1:
figure 1
2. Steps
Tips: The following codes are completed by checking the page source code and reviewing the elements.
2.1. Click to purchase tickets
driver.switch_to.window(driver.window_handles[-1])
time.sleep(3)
# 刷新
driver.refresh()
time.sleep(3)
print(driver.title)
print(driver.current_url)
# https://kyfw.12306.cn/otn/view/index.html
# 在新窗口点击前往订票,又会跳转到新窗口
driver.find_element(By.XPATH, '//*[@id="link_for_ticket"]').click()
2.2. Enter the train information in the search box
question:
There is a problem here. To enter content directly in the input box, you must select the destination prompted by the system to succeed, and this destination cannot be reviewed;
Solution:
First click the input box, enter the content, then click another input box, and repeat the cycle (after testing, the prompts in the date input box are ignored), and the prompt content that appears in the system will be selected by default.
driver.switch_to.window(driver.window_handles[-1])
time.sleep(5)
# 出发地
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').click()
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').send_keys("深圳北", Keys.ENTER)
# 目的地
time.sleep(1)
driver.find_element(By.XPATH, '//*[@id="toStationText"]').click()
driver.find_element(By.XPATH, '//*[@id="toStationText"]').send_keys("百色", Keys.ENTER)
# 日期,先清除默认内容,在输入
driver.find_element(By.XPATH, '//*[@id="train_date"]').click()
driver.find_element(By.XPATH, '//*[@id="train_date"]').clear()
# time.sleep(1)
driver.find_element(By.XPATH, '//*[@id="train_date"]').send_keys("2023-01-18")
2.3. Click to find
time.sleep(2)
driver.find_element(By.XPATH, '//*[@id="query_ticket"]').click()
2.4. When the train information appears, filter it. If there are tickets, click on the ticket booking interface.
for tr in tr_list: # 遍历所有车次信息
time.sleep(1)
train_number = tr.find_element(By.CLASS_NAME, "number").text # 车次
if train_number in trains: # 如果该车次在输入的车次里
left_ticker_td = tr.find_element(By.XPATH, './/td[4]').text
if left_ticker_td == '有' or left_ticker_td.isdigit(): # 如果该车次有票
print(train_number+'有票')
btn72 = tr.find_element(By.CLASS_NAME, 'btn72') # 找到该车次的预订按钮
btn72.click()
3. Results
3.1. The final result of running the code
As shown in Figure 2 below:
figure 2
4. Sections
This part is the second part. It completes the train search from the login interface and filters the trains. The remaining part is to confirm the passenger information and submit the order to complete the automated ticket grabbing.
What you need to know to complete this part:
1) Selenium’s perspective switching, clicking, text box input, and content extraction
2) python basics
Note: The complete code will be released in the third part to fully realize the function.