• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

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

在知乎上看到了一个关于携程网机票查询的爬虫程序 :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()

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

r 放 try 外面定义试试

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!