程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Welcome home at any time - Python makes a version of train ticket grabbing (with complete code, for learning reference only)

編輯:Python

Hey, everyone, good duck ~ I'm a panda

Today I will teach you how to use Python To make a 12306 Ticket checking program script ( For reference only )


what are you having? python I won't answer the related error report 、 Or source code information / Module installation / Women's clothing bosses are proficient in skills You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me ( At the end of the article )

First , First import the required modules

import requests
import pandas as pd
import json

Request data

Find the source of the data

url = 'https://kyfw.12306.cn/otn/leftTicket/query'
data = {

'leftTicketDTO.train_date': train_date,
'leftTicketDTO.from_station': city_json[from_station],
'leftTicketDTO.to_station': city_json[to_station],
'purpose_codes': 'ADULT',
}
headers = {

'Cookie': '_uab_collina=163108019860709243490927; JSESSIONID=3A879F34238B594124705B10D7C0B0E6; BIGipServerotn=3956736266.64545.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; BIGipServerpassport=870842634.50215.0000; RAIL_EXPIRATION=1631354049020; RAIL_DEVICEID=jY49UGp1PWZZ0cY6CWj2wmKFDH60qsPXbu7L4D2DjNDJSM4sbqZmmlUm62-6L3k9SNtBAUgBPn7Rh1-FAxka97-nHNpT3QIh5YIXtw3mGao0mjLNkIv2ayvwqxWyFhdbos5_ziUA3XVil7awDZ0EjzKBAWdl22Hu; route=495c805987d0f5c8c84b14f60212447d; _jc_save_fromStation=%u957F%u6C99%2CCSQ; _jc_save_fromDate=2021-09-08; _jc_save_toDate=2021-09-08; _jc_save_wfdc_flag=dc; _jc_save_toStation=%u5CB3%u9633%u4E1C%2CYIQ',
# 'Host': 'kyfw.12306.cn',
# 'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E9%95%BF%E6%B2%99,CSQ&ts=%E5%B2%B3%E9%98%B3%E4%B8%9C,YIQ&date=2021-09-08&flag=N,N,Y',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url=url, params=data, headers=headers)

Automatic identification code

response.encoding = response.apparent_encoding

Data analysis value

 for index in result:
# requests.utils.unquote(index)
index_list = index.replace(' Yes ', 'Yes').replace(' nothing ', 'No').split('|')
# print(index_list)
train_number = index_list[3] # Train number 
if 'G' in train_number:
time_1 = index_list[8] # Departure time 
time_2 = index_list[9] # Arrival time 
prince_seat = index_list[32] # First class 
first_class_seat = index_list[31] # First class seat 
second_class = index_list[30] # Second-class seat 
dit = {

'Num': train_number,
'Start': time_1,
'End': time_2,
'Top': prince_seat,
'First': prince_seat,
'Second': second_class,
'Yz': '-',
'Wz': '-',
'Rw': '-',
'Yw': '-',
}
else:
time_1 = index_list[8] # Departure time 
time_2 = index_list[9] # Arrival time 
hard_seat = index_list[29] # Hard seat 
no_seat = index_list[26] # No seat 
soft_sleeper = index_list[23] # Soft sleeper 
hard_sleeper = index_list[28] # Hard sleeper 
dit = {

'Num': train_number,
'Start': time_1,
'End': time_2,
'Top': '-',
'First': '-',
'Second': '-',
'Yz': hard_seat,
'Wz': no_seat,
'Rw': soft_sleeper,
'Yw': hard_sleeper,
}
lis.append(dit)
content = pd.DataFrame(lis)
print(content)

Realize the query of train number data of a city

Realize the national train number query

f = open('city.json', mode='r', encoding='utf-8')
text = f.read()
city_json = json.loads(text)
while True:
from_station = input(' Please enter your departure city : ')
to_station = input(' Please enter the city you reached : ')
train_date = input(' Please enter the travel time (2021-09-08/ year - month - Japan ):')

Query again / sign out

again = input(' Continue to query (Y/N): ')
if again == 'Y' or again == 'y':
continue
else:
break

Complete code

python Learning exchange group :660193417###
import requests
import pandas as pd
import json
f = open('city.json', mode='r', encoding='utf-8')
text = f.read()
city_json = json.loads(text)
while True:
from_station = input(' Please enter your departure city : ')
to_station = input(' Please enter the city you reached : ')
train_date = input(' Please enter the travel time (2021-09-08/ year - month - Japan ):')
url = 'https://kyfw.12306.cn/otn/leftTicket/query'
data = {

'leftTicketDTO.train_date': train_date,
'leftTicketDTO.from_station': city_json[from_station],
'leftTicketDTO.to_station': city_json[to_station],
'purpose_codes': 'ADULT',
}
headers = {

'Cookie': '_uab_collina=163108019860709243490927; JSESSIONID=3A879F34238B594124705B10D7C0B0E6; BIGipServerotn=3956736266.64545.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; BIGipServerpassport=870842634.50215.0000; RAIL_EXPIRATION=1631354049020; RAIL_DEVICEID=jY49UGp1PWZZ0cY6CWj2wmKFDH60qsPXbu7L4D2DjNDJSM4sbqZmmlUm62-6L3k9SNtBAUgBPn7Rh1-FAxka97-nHNpT3QIh5YIXtw3mGao0mjLNkIv2ayvwqxWyFhdbos5_ziUA3XVil7awDZ0EjzKBAWdl22Hu; route=495c805987d0f5c8c84b14f60212447d; _jc_save_fromStation=%u957F%u6C99%2CCSQ; _jc_save_fromDate=2021-09-08; _jc_save_toDate=2021-09-08; _jc_save_wfdc_flag=dc; _jc_save_toStation=%u5CB3%u9633%u4E1C%2CYIQ',
# 'Host': 'kyfw.12306.cn',
# 'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E9%95%BF%E6%B2%99,CSQ&ts=%E5%B2%B3%E9%98%B3%E4%B8%9C,YIQ&date=2021-09-08&flag=N,N,Y',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url=url, params=data, headers=headers)
response.encoding = response.apparent_encoding
result = response.json()['data']['result']
lis = []
for index in result:
# requests.utils.unquote(index)
index_list = index.replace(' Yes ', 'Yes').replace(' nothing ', 'No').split('|')
# print(index_list)
train_number = index_list[3] # Train number 
if 'G' in train_number:
time_1 = index_list[8] # Departure time 
time_2 = index_list[9] # Arrival time 
prince_seat = index_list[32] # First class 
first_class_seat = index_list[31] # First class seat 
second_class = index_list[30] # Second-class seat 
dit = {

'Num': train_number,
'Start': time_1,
'End': time_2,
'Top': prince_seat,
'First': prince_seat,
'Second': second_class,
'Yz': '-',
'Wz': '-',
'Rw': '-',
'Yw': '-',
}
else:
time_1 = index_list[8] # Departure time 
time_2 = index_list[9] # Arrival time 
hard_seat = index_list[29] # Hard seat 
no_seat = index_list[26] # No seat 
soft_sleeper = index_list[23] # Soft sleeper 
hard_sleeper = index_list[28] # Hard sleeper 
dit = {

'Num': train_number,
'Start': time_1,
'End': time_2,
'Top': '-',
'First': '-',
'Second': '-',
'Yz': hard_seat,
'Wz': no_seat,
'Rw': soft_sleeper,
'Yw': hard_sleeper,
}
lis.append(dit)
pd.set_option('display.max_rows', None)
content = pd.DataFrame(lis)
print(content)
again = input(' Continue to query (Y/N): ')
if again == 'Y' or again == 'y':
continue
else:
break

That's all for today's article , I hope it will be helpful to you who are learning by yourself ~

I'm a panda , See you in the next article (*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved