航班信息数据获取(传入起始点、终点以及日期)

北航软院大三小学期太离谱啦!
顶不住啦

进入正题

获取航班信息

声明: 该方法完全免费,但仅供非商业用途!调用的接口官网在此

代码:

import requests
from lxml import html

etree = html.etree

def get_xml(url, start_city, last_city, date):
    resp = requests.get(url, params={"startCity":start_city, "lastCity":last_city, "theDate":date, "userID":""})
    resp.encoding = "utf-8"
    return resp.content

def parse_xml(xml_text):
    xml = etree.XML(xml_text)
    airlines = xml.xpath("//AirlinesTime")
    result = []
    for airline in airlines:
        item = {}
        company = airline.xpath("./Company/text()")[0]
        item['company'] = company
        airline_code = airline.xpath("./AirlineCode/text()")[0]
        item['airline_code'] = airline_code
        start_drome = airline.xpath("./StartDrome/text()")[0]
        item['start_drome'] = start_drome
        arrive_drome = airline.xpath("./ArriveDrome/text()")[0]
        item['arrive_drome'] = arrive_drome
        start_time = airline.xpath("./StartTime/text()")[0]
        item['start_time'] = start_time
        arrive_time = airline.xpath("./ArriveTime/text()")[0]
        item['arrive_time'] = arrive_time
        mode = airline.xpath("./Mode/text()")[0]
        item['mode'] = mode
        airline_stop = airline.xpath("./AirlineStop/text()")[0]
        item['airline_stop'] = airline_stop
        week = airline.xpath("./Week/text()")[0]
        item['week'] = week
        result.append(item)
    return result


if __name__ == '__main__':
    url = "http://www.webxml.com.cn/webservices/DomesticAirline.asmx/getDomesticAirlinesTime"
    start_city = "北京"
    last_city = "上海"
    date = "2021-07-11"
    xml_text = get_xml(url, start_city, last_city, date)
    print(parse_xml(xml_text))

运行效果:
(ps:我将结果复制到JsonView中格式化展示了)
在这里插入图片描述
字段解释:

  • company:航空公司
  • airline_code:航班号
  • start_drome:起飞机场
  • arrive_drome:降落机场
  • start_time:起飞时间
  • arrive_time:到达时间
  • mode:机型
  • airline_stop:经停站数量(似乎查到的都是0)
  • week:一周里起飞的星期数,如“246日”表示周二、周四、周六、周日可以起飞

ps:

  • 该官网提供了许多服务,详情可以参考这篇博客
  • lxml库是一个专门用于解析html、xml等标签语言的第三方库,十分好用,但有一定的学习成本,可以到这篇博客快速上手
  • 该接口返回的是xml文本,而不是常见的json格式,因此我对其的操作与爬虫的操作类似。(爬虫就是获取html文本并解析嘛)如下:
  • 在这里插入图片描述

博主qq:621066455,如遇到问题可以加好友交流,或者直接在评论区交流(之前一篇博客过了四个月才看评论,感觉好对不起感谢、支持我的朋友们/(ㄒoㄒ)/~~)

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值