Implementation of Ctrip ticket query in Python

Keywords: Python JSON Spring Database

I used to refer to other people's code and made a 12306 command-line train ticket query tool in Python. It's very interesting! So I made a similar - Ctrip ticket finder.

The effect of Ctrip's official website query is as follows:

The effect of Python command line interface query is as follows:

Enter the departure, destination and boarding date to see the optional flight, airport, departure and arrival time, ticket price and other information.

The source code of the program is as follows:

1.air_stations.py

2.airline_ticket.py

 1 #1.air_stations.py
 2 import re
 3 import os
 4 import json
 5 import requests
 6 from pprint import pprint
 7 
 8 url = 'http://webresource.c-ctrip.com/code/cquery/resource/address/flight/flight_new_poi_gb2312.js?CR_2017_07_18_00_00_00'
 9 response = requests.get(url,verify=False)
10 station = re.findall(u'([\u4e00-\u9fa5]+)\(([A-Z]+)\)', response.text)
11 stations = dict(station)
12 pprint(stations,indent = 4)
 1 2.airline_ticket.py
 2 #This program can be used to query Ctrip tickets, query need to specify the departure date, departure city, destination city! (imitating 12306 train ticket booking query procedure) Python Learning group 125240963 update learning materials every day and access dozens of sets PDF E-book
 3 import requests,json,os
 4 from docopt import docopt
 5 from prettytable import PrettyTable
 6 from colorama import init,Fore
 7 from air_stations import stations
 8 
 9 fromCity = input('Please input the city you want leave :')
10 toCity = input('Please input the city you will arrive :')
11 tripDate = input('Please input the date(Example:2017-09-27) :')
12 
13 init()
14 class TrainsCollection:
15     header = 'Airline flight airport time ticket price airport construction cost'.split()
16     def __init__(self,airline_tickets):
17         self.airline_tickets = airline_tickets
18 
19     @property
20     def plains(self):
21         #We didn't find the general list of airlines, but most of the common Airlines don't use it for the time being dict{air_company}To collect!
22         #If strs If the query is not successful, a KeyError,Express this dict If the target airline is not found in, it will be displayed with its English code!
23         air_company = {"G5":"Cathay Airlines","9C":"Spring Airlines","MU":"Eastern Airlines","NS":"Hebei Airlines","HU":"Hainan Airlines","HO":"Lucky airline","CZ":"Southern Airlines","FM":"Shanghai Airlines","ZH":"Shenzhen Airlines","MF":"Xiamen Airlines","CA":"Air China","KN":"China United Airlines"}
24         for item in self.airline_tickets:
25             try:
26                 strs = air_company[item['alc']]
27             except KeyError:
28                 strs = item['alc']
29             airline_data = [
30             Fore.BLUE + strs + Fore.RESET,
31             Fore.BLUE + item['fn'] + Fore.RESET,
32             '\n'.join([Fore.YELLOW + item['dpbn'] + Fore.RESET,
33                        Fore.CYAN + item['apbn'] + Fore.RESET]),
34             '\n'.join([Fore.YELLOW + item['dt'] + Fore.RESET,
35                        Fore.CYAN + item['at'] + Fore.RESET]),
36             item['lp'],
37             item['tax'],
38             ]
39             yield airline_data
40 
41     def pretty_print(self):
42         #PrettyTable()Used to print the queried flight information table to the terminal line by line on the screen
43         pt = PrettyTable()
44         pt._set_field_names(self.header)
45         for airline_data in self.plains:
46             pt.add_row(airline_data)
47         print(pt)
48 
49 def doit():
50     headers = {
51         "Cookie":"custom",
52         "User-Agent": "custom",
53         }
54     arguments = {
55     'from':fromCity,
56     'to':toCity,
57     'date':tripDate
58     }
59     DCity1 = stations[arguments['from']]
60     ACity1 = stations[arguments['to']]
61     DDate1 = arguments['date']
62     url = ("http://flights.ctrip.com/domesticsearch/search/SearchFirstRouteFlights?DCity1={}&ACity1={}&SearchType=S&DDate1={}").format(DCity1,ACity1,DDate1)
63     try:
64         r = requests.get(url,headers = headers,verify=False)
65     except Exception as e:
66         print(repr(e))
67     print(url)
68     airline_tickets = r.json()['fis']
69     TrainsCollection(airline_tickets).pretty_print()
70 
71 if __name__ == '__main__':
72     doit()

In fact, this small program can also be expanded, such as saving the query records to the local computer (in the format of txt, or in the database) or more powerful, you can also set the timing of automatic query; you can also set the query to automatically send email reminders; you can also use the Python GUI library to make this program into the form of desktop software....

 

Learn to program, there are many benefits A kind of

Posted by usefulphp on Tue, 21 Jan 2020 09:11:10 -0800