航班信息数据获取(传入起始点、终点以及日期) | 您所在的位置:网站首页 › opensky平台官网 › 航班信息数据获取(传入起始点、终点以及日期) |
北航软院大三小学期太离谱啦! 顶不住啦! 进入正题 获取航班信息声明: 该方法完全免费,但仅供非商业用途!调用的接口官网在此 代码: 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中格式化展示了) ps: 该官网提供了许多服务,详情可以参考这篇博客lxml库是一个专门用于解析html、xml等标签语言的第三方库,十分好用,但有一定的学习成本,可以到这篇博客快速上手该接口返回的是xml文本,而不是常见的json格式,因此我对其的操作与爬虫的操作类似。(爬虫就是获取html文本并解析嘛)如下:![]() 博主qq:621066455,如遇到问题可以加好友交流,或者直接在评论区交流(之前一篇博客过了四个月才看评论,感觉好对不起感谢、支持我的朋友们/(ㄒoㄒ)/~~) |
CopyRight 2018-2019 实验室设备网 版权所有 |