You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
4.3 KiB
89 lines
4.3 KiB
import requests
|
|
# 导入表格输出
|
|
import prettytable as pt
|
|
# 导入json
|
|
import json
|
|
import pymysql
|
|
from config import mysql_local
|
|
|
|
|
|
class chepiao_info:
|
|
def __init__(self):
|
|
self.conn = pymysql.connect(**mysql_local)
|
|
self.cursor = self.conn.cursor()
|
|
|
|
|
|
|
|
def save_data(self, num, start_time, end_time, use_time, topGrade, first_class, second_class, soft_sleeper, hard_sleeper, hard_seat, no_seat):
|
|
data = (num, start_time, end_time, use_time, topGrade, first_class, second_class, soft_sleeper, hard_sleeper, hard_seat, no_seat)
|
|
sql = "INSERT INTO chepiao_info (num, start_time, end_time, use_time, topGrade, first_class, second_class, soft_sleeper, hard_sleeper, hard_seat, no_seat) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
self.cursor.execute(sql, data)
|
|
self.conn.commit()
|
|
|
|
def Ticket(start_city, end_city, date):
|
|
# # 查票 输入 出发城市 / 目的城市 / 出发时间
|
|
# start_city = input('输入出发城市:') # 输入函数使用方法
|
|
# end_city = input('输入目的城市:')
|
|
# date = input('输入出发时间<2022-08-06>:')
|
|
# 读取城市json文件
|
|
f = open('city.json', encoding='utf-8') # 文件读取
|
|
# f.read() 字符串
|
|
city_json = json.loads(f.read()) # 数据容器转换
|
|
# 出发城市
|
|
from_station = city_json[start_city] # 字典取值
|
|
# 目的城市
|
|
to_station = city_json[end_city] # 字典取值
|
|
# 确定请求url
|
|
url = f'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={date}&leftTicketDTO.from_station={from_station}&leftTicketDTO.to_station={to_station}&purpose_codes=ADULT'
|
|
# 模拟浏览器
|
|
headers = {
|
|
'Cookie': '_uab_collina=165650330916153394558455; JSESSIONID=4E8C54B97C3C4A2836EFB3A6529F2FA7; _jc_save_wfdc_flag=dc; _jc_save_fromStation=%u7941%u9633%2CQWQ; _jc_save_toStation=%u957F%u6C99%2CCSQ; BIGipServerotn=535822858.24610.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; BIGipServerpool_passport=98828810.50215.0000; RAIL_EXPIRATION=1666352913084; RAIL_DEVICEID=UUAN3j96v_gsz37OlhGUDBYABtIVZXLmp5TFSAEwzFBx_tqx8r9Ar-jZUTup-K6STKZxdcyuK-7PqGLIOkQCxWsN8B6yF8HYHi0vxUkHWP8pbJgCCEnFwbbJ6tEYIVFOEZEQTBtCXtronC-e8nuewGsPhY725hV_; route=6f50b51faa11b987e576cdb301e545c4; _jc_save_fromDate=2022-10-20; _jc_save_toDate=2022-10-18',
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36',
|
|
|
|
}
|
|
# 发送请求 <Response [200]> 表示对于url地址发送请求,请求成功
|
|
response = requests.get(url=url, headers=headers)
|
|
# 2. 获取数据, 获取服务器返回响应数据 ---> 开发者工具里面response print(response.json())
|
|
# 序号
|
|
lis = []
|
|
page = 0
|
|
c = chepiao_info()
|
|
# 3. 解析数据, 获取 response.json() json字典数据, 字典取值, 键值对取值, 根据冒号左边, 提取冒号右边
|
|
for index in response.json()['data']['result']:
|
|
# split() 字符串分割方法 --> 返回是列表
|
|
content_list = index.split('|')
|
|
# 根据列表索引位置提取内容
|
|
num = content_list[3] # 车次
|
|
start_time = content_list[8] # 出发时间
|
|
end_time = content_list[9] # 到达时间
|
|
use_time = content_list[10] # 耗时
|
|
topGrade = content_list[25] # 特等座
|
|
if topGrade:
|
|
pass
|
|
else:
|
|
topGrade = content_list[32]
|
|
first_class = content_list[31] # 一等
|
|
second_class = content_list[30] # 二等
|
|
hard_sleeper = content_list[28] # 硬卧
|
|
hard_seat = content_list[29] # 硬座
|
|
no_seat = content_list[26] # 无座
|
|
soft_sleeper = content_list[23] # 软卧
|
|
dit = {
|
|
'SerialNo': page,
|
|
'TravelNumber': num,
|
|
'DepartureTime': start_time,
|
|
'ArrivalTime': end_time,
|
|
'TimeTime': use_time,
|
|
'PremiumSeat': topGrade,
|
|
'FirstClassSeat': first_class,
|
|
'SecondClassSeat': second_class,
|
|
'SoftSleeper': soft_sleeper,
|
|
'HardSleeper': hard_sleeper,
|
|
'HardSeat': hard_seat,
|
|
'Noseat': no_seat,
|
|
}
|
|
lis.append(dit)
|
|
page += 1
|
|
c.save_data(num, start_time, end_time, use_time, topGrade, first_class, second_class, soft_sleeper, hard_sleeper, hard_seat, no_seat)
|
|
return lis
|