前言
有人想去武汉,重庆,成都玩的吗,我五一想去,但是奈何公司假期一直没有公布,具体假期耶不知道几天,结果今晚上知道了,然后马上点开我的12306,没票了…我直接裂开
也不是完全没票,需要候补,还是可以试着抢一抢,但是我的手拿抢的过脚本,还是得靠我的python
okok,话不多说 咱就直接开始吧
导入模块
import requests
import pandas as pd
import json
请求数据
找到数据来源
url = 'https:///otn/leftTicket/query'
data = {完整源码领取+v:xiaoyuanllsll
'leftTicketDTO.train_date': train_date,
'leftTicketDTO.from_station': city_json[from_station],
'leftTicketDTO.to_station': city_json[to_station],
'purpose_codes': 'ADULT',
}
headers = {
'Cookie': '',
# '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
数据解析取值
for index in result:
# requests.utils.unquote(index)
index_list = index.replace('有', 'Yes').replace('无', 'No').split('|')
# print(index_list)
train_number = index_list[3] # 车次
if 'G' in train_number:
time_1 = index_list[8] # 出发时间
time_2 = index_list[9] # 到达时间
prince_seat = index_list[32] # 特等座
first_class_seat = index_list[31] # 一等座
second_class = index_list[30] # 二等座
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] # 出发时间
time_2 = index_list[9] # 到达时间
hard_seat = index_list[29] # 硬座
no_seat = index_list[26] # 无座
soft_sleeper = index_list[23] # 软卧
hard_sleeper = index_list[28] # 硬卧
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)
实现查询一座城市车次数据
实现全国车次查询
f = open('city.json', mode='r', encoding='utf-8')
text = f.read()
city_json = json.loads(text)
while True:
from_station = input('请输入你出发的城市: ')
to_station = input('请输入你达到的城市: ')
train_date = input('请输入乘车时间(2023-04-023/年-月-日):')
再次查询/退出
again = input('是否继续查询(Y/N): ')
if again == 'Y' or again == 'y':
continue
else:
break
完整代码
import requests
import pandas as pd完整源码领取+v:xiaoyuanllsll
import json
f = open('city.json', mode='r', encoding='utf-8')
text = f.read()
city_json = json.loads(text)
while True:
from_station = input('请输入你出发的城市: ')
to_station = input('请输入你达到的城市: ')
train_date = input('请输入乘车时间(2023-04-23/年-月-日):')
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': '',
# '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').replace('无', 'No').split('|')
# print(index_list)
train_number = index_list[3] # 车次
if 'G' in train_number:
time_1 = index_list[8] # 出发时间
time_2 = index_list[9] # 到达时间
prince_seat = index_list[32] # 特等座
first_class_seat = index_list[31] # 一等座
second_class = index_list[30] # 二等座
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] # 出发时间
time_2 = index_list[9] # 到达时间
hard_seat = index_list[29] # 硬座
no_seat = index_list[26] # 无座
soft_sleeper = index_list[23] # 软卧
hard_sleeper = index_list[28] # 硬卧
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('是否继续查询(Y/N): ')
if again == 'Y' or again == 'y':
continue
else:
break
抢票代码
点击文末名片领取~
最后
很多小伙伴在学习python的时候总会遇到一些问题和瓶颈,没有方向感,不知道该从哪里入手去提升,所以我准备了一些资料,都可以分享大家的
请看下方文字~