携程网机票查询脚本,无法正常运行,麻烦大家看看错在哪里.....

在知乎上看到了一个关于携程网机票查询的爬虫程序 :https://zhuanlan.zhihu.com/p/33148780 尝试着在本机运行时候,发现在运行airline_ticket.py文件的时候出现报错:

https://img3.mukewang.com/5bb55cf70001038c08460295.jpg

【代码脚本】如下:

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()

慕勒13948
浏览 1398回答 3
3回答

鹹魚Yu

r 放 try 外面定义试试
打开App,查看更多内容
随时随地看视频慕课网APP