在知乎上看到了一個(gè)關(guān)于攜程網(wǎng)機(jī)票查詢的爬蟲程序 :https://zhuanlan.zhihu.com/p/33148780?嘗試著在本機(jī)運(yùn)行時(shí)候,發(fā)現(xiàn)在運(yùn)行airline_ticket.py文件的時(shí)候出現(xiàn)報(bào)錯(cuò):【代碼腳本】如下:import requests,json,osfrom docopt import docoptfrom prettytable import PrettyTablefrom colorama import init,Forefrom air_stations import stationsfromCity = 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 = '航空公司 航班 機(jī)場(chǎng) 時(shí)間 機(jī)票價(jià)格 機(jī)場(chǎng)建設(shè)費(fèi)'.split()? def __init__(self,airline_tickets):? ? ?self.airline_tickets = airline_tickets@propertydef plains(self):? air_company = {"G5":"華夏航空","9C":"春秋航空","MU":"東方航空","NS":"河北航空","HU":"海南航空","HO":"吉祥航空","CZ":"南方航空","FM":"上海航空","ZH":"深圳航空","MF":"廈門航空","CA":"中國(guó)國(guó)航","KN":"中國(guó)聯(lián)航"}? 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_datadef 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()
- 3 回答
- 0 關(guān)注
- 1475 瀏覽
添加回答
舉報(bào)
0/150
提交
取消