import requests
import json
from prettytable import PrettyTable
class AirlineCollection:
header = '航空公司 航班 出发地 出发机场 出发时间 到达地 到达机场 到达时间 价格 余票'.split()
def __init__(self, airline_tickets):
self.airline_tickets = airline_tickets
@property
def plains(self):
for item in self.airline_tickets:
airline_data = [
item["airlineName"],
item["flightNumber"],
item["departureCityName"],
item["departureAirportName"] + item["departureTerminalName"],
item["departureDate"],
item["arrivalCityName"],
item["arrivalAirportName"] + item["arrivalTerminalName"],
item["arrivalDate"],
# '\n'.join([item["departureAirportName"] + item["departureTerminalName"],
# item["arrivalAirportName"] + item["arrivalTerminalName"]]),
# '\n'.join([item["departureDate"],
# item["arrivalDate"]]),
item["price"],
item["seatCount"]
]
yield airline_data
def pretty_print(self):
pt = PrettyTable()
pt.field_names = self.header
for airline_data in self.plains:
pt.add_row(airline_data)
pt.align = "c"
print(pt.get_string(sortby="出发时间"))
def getResponse(fromCity, toCity, tripDate):
url = "https://flights.ctrip.com/itinerary/api/12808/products"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0",
"Referer": "https://flights.ctrip.com/international/search/",
"Content-Type": "application/json"
}
request_payload = {
"flightWay": "Oneway",
"classType": "ALL",
"hasChild": False,
"hasBaby": False,
"searchIndex": 1,
"airportParams": [
{"dcity": fromCity, "acity": toCity, "date": tripDate}
]
}
# post请求
response = requests.post(url, data=json.dumps(request_payload), headers=headers).text
return response
def parseInfo(fromCity, toCity, tripDate):
airInfoList = []
response = getResponse(fromCity, toCity, tripDate)
# 很多航班信息在此分一下
routeList = json.loads(response).get('data').get('routeList')
# 依次读取每条信息
for route in routeList:
# 判断是否有信息,有时候没有会报错
airplaneInfo = {}
if len(route.get('legs')) == 1:
legs = route.get('legs')
flight = legs[0].get('flight')
# 提取想要的信息
airlineName = flight.get('airlineName') # 航空公司
flightNumber = flight.get('flightNumber') # 航班编号
departureDate = flight.get('departureDate') # 出发时间
arrivalDate = flight.get('arrivalDate') # 到达时间
craftTypeName = flight.get('craftTypeName') # 飞机类型
craftTypeKindDisplayName = flight.get('craftTypeKindDisplayName') # 飞机型号:大型;中型,小型
departureCityName = flight.get('departureAirportInfo').get('cityName') # 出发城市
departureAirportName = flight.get('departureAirportInfo').get('airportName') # 出发机场名称
departureTerminalName = flight.get('departureAirportInfo').get('terminal').get('name') # 出发机场航站楼
arrivalCityName = flight.get('arrivalAirportInfo').get('cityName') # 到达城市
arrivalAirportName = flight.get('arrivalAirportInfo').get('airportName') # 到达机场名称
arrivalTerminalName = flight.get('arrivalAirportInfo').get('terminal').get('name') # 到达机场航站楼
punctualityRate = flight.get('punctualityRate') # 到达准点率
mealType = flight.get('mealType') # 是否有餐食 None:代表无餐食,Snack:代表小食,Meal:代表含餐食
cabins = legs[0].get('cabins')
price = cabins[0].get('price').get('price') # 标准价格
rate = cabins[0].get('price').get('rate') # 折扣率
seatCount = cabins[0].get('seatCount') # 剩余座位数
refundEndorse = cabins[0].get('refundEndorse').get('minRefundFee') # 成人票:产品退订费
minEndorseFee = cabins[0].get('refundEndorse').get('minRefundFee') # 成人票:产品更改费
endorseNote = cabins[0].get('refundEndorse').get('endorseNote') # 成人票:签转条件
freeLuggageAmount = cabins[0].get('freeLuggageAmount') # 免费托运重量
carryonLuggageMaxAmount = cabins[0].get('luggageLimitation').get('carryonLuggageMaxAmount')
# 允许携带手提行李最大数量 0:代表无免费行李额,1:代表一件,-2:代表不限件数
carryonLuggageMaxWeight = cabins[0].get('luggageLimitation').get('carryonLuggageMaxWeight') # 允许携带手提行李最大重量
carryonLuggageMaxSize = cabins[0].get('luggageLimitation').get('carryonLuggageMaxSize') # 允许携带手提行李最大规格
checkinLuggageMaxAmount = cabins[0].get('luggageLimitation').get('checkinLuggageMaxAmount')
# 允许托运的行李最大数量类型 0:代表无免费行李额,1:代表一件,-2:代表不限件数
checkinLuggageMaxWeight = cabins[0].get('luggageLimitation').get('checkinLuggageMaxWeight') # 允许托运的行李最大重量
checkinLuggageMaxSize = cabins[0].get('luggageLimitation').get('checkinLuggageMaxSize') # 允许托运的行李最大规格
characteristic = legs[0].get('characteristic')
lowestPrice = characteristic.get('lowestPrice') # 成人经济舱最低价
lowestCfPrice = characteristic.get('lowestCfPrice') # 成人公务舱最低价
lowestChildPrice = characteristic.get('lowestChildPrice') # 儿童经济舱最低价
lowestChildCfPrice = characteristic.get('lowestChildCfPrice') # 儿童公务舱最低价
# 将数据放入字典
airplaneInfo["airlineName"] = airlineName
airplaneInfo["flightNumber"] = flightNumber
airplaneInfo["departureDate"] = departureDate
airplaneInfo["arrivalDate"] = arrivalDate
airplaneInfo["craftTypeName"] = craftTypeName
airplaneInfo["craftTypeKindDisplayName"] = craftTypeKindDisplayName
airplaneInfo["departureCityName"] = departureCityName
airplaneInfo["departureAirportName"] = departureAirportName
airplaneInfo["departureTerminalName"] = departureTerminalName
airplaneInfo["arrivalCityName"] = arrivalCityName
airplaneInfo["arrivalAirportName"] = arrivalAirportName
airplaneInfo["arrivalTerminalName"] = arrivalTerminalName
airplaneInfo["punctualityRate"] = punctualityRate
airplaneInfo["mealType"] = mealType
airplaneInfo["price"] = price
airplaneInfo["rate"] = rate
airplaneInfo["seatCount"] = seatCount
airplaneInfo["refundEndorse"] = refundEndorse
airplaneInfo["minEndorseFee"] = minEndorseFee
airplaneInfo["endorseNote"] = endorseNote
airplaneInfo["freeLuggageAmount"] = freeLuggageAmount
airplaneInfo["carryonLuggageMaxAmount"] = carryonLuggageMaxAmount
airplaneInfo["carryonLuggageMaxWeight"] = carryonLuggageMaxWeight
airplaneInfo["carryonLuggageMaxSize"] = carryonLuggageMaxSize
airplaneInfo["checkinLuggageMaxAmount"] = checkinLuggageMaxAmount
airplaneInfo["checkinLuggageMaxWeight"] = checkinLuggageMaxWeight
airplaneInfo["checkinLuggageMaxSize"] = checkinLuggageMaxSize
airplaneInfo["lowestPrice"] = lowestPrice
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载体验!下载完使用问题请私信沟通。 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【资源说明】 基于Python实现的携程机票信息查询系统源码+详细注释.zip基于Python实现的携程机票信息查询系统源码+详细注释.zip基于Python实现的携程机票信息查询系统源码+详细注释.zip基于Python实现的携程机票信息查询系统源码+详细注释.zip 基于Python实现的携程机票信息查询系统源码+详细注释.zip 基于Python实现的携程机票信息查询系统源码+详细注释.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于Python实现的携程机票信息查询系统源码+详细注释.zip (3个子文件)
code
airline_ticket.py 8KB
main.py 652B
air_stations.py 297B
共 3 条
- 1
资源评论
北航程序员小C
- 粉丝: 2222
- 资源: 1823
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功