在知乎上看到了一个关于携程网机票查询的爬虫程序 :https://zhuanlan.zhihu.com/p/33148780 尝试着在本机运行时候,发现在运行airline_ticket.py文件的时候出现报错:
【代码脚本】如下:
import requests,json,os
from docopt import docopt
from prettytable import PrettyTable
from colorama import init,Fore
from air_stations import stations
fromCity = input('Please input the city you want leave :')
toCity = input('Please input the city you will arrive :')
tripDate = input('Please input the date(Example:2018-10-29) :')
init()
class TrainsCollection:
header = '航空公司 航班 机场 时间 机票价格 机场建设费'.split()
def __init__(self,airline_tickets):
self.airline_tickets = airline_tickets
@property
def plains(self):
air_company = {"G5":"华夏航空","9C":"春秋航空","MU":"东方航空","NS":"河北航空","HU":"海南航空","HO":"吉祥航空","CZ":"南方航空","FM":"上海航空","ZH":"深圳航空","MF":"厦门航空","CA":"中国国航","KN":"中国联航"}
for item in self.airline_tickets:
try:
strs = air_company[item['alc']]
except KeyError:
strs = item['alc']
airline_data = [
Fore.BLUE + strs + Fore.RESET,
Fore.BLUE + item['fn'] + Fore.RESET,
'\n'.join([Fore.YELLOW + item['dpbn'] + Fore.RESET,
Fore.CYAN + item['apbn'] + Fore.RESET]),
'\n'.join([Fore.YELLOW + item['dt'] + Fore.RESET,
Fore.CYAN + item['at'] + Fore.RESET]),
item['lp'],
item['tax'],
]
yield airline_data
def pretty_print(self):
pt = PrettyTable()
pt._set_field_names(self.header)
for airline_data in self.plains:
pt.add_row(airline_data)
print(pt)
def doit():
headers = {
"Cookie":"自定义",
"User-Agent": "自定义",
}
arguments = {
'from':fromCity,
'to':toCity,
'date':tripDate
}
DCity1 = stations[arguments['from']]
ACity1 = stations[arguments['to']]
DDate1 = arguments['date']
url=("http://flights.ctrip.com/domesticsearch/search/SearchFirstRouteFlights?DCity1={}&ACity1={}&SearchType=S&DDate1={}").format(DCity1,ACity1,DDate1)
try:
r = requests.get(url,headers = headers,verify=False)
except Exception as e:
print(repr(e))
print(url)
airline_tickets = r.json()['fis']
TrainsCollection(airline_tickets).pretty_print()
if __name__ == '__main__':
doit()
鹹魚Yu