• 大小: 1KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: Python
  • 标签: python  获取  

资源简介

python 实现了获取去哪儿网火车票的信息。

资源截图

代码片段和文件信息

#coding=utf-8
import requests
import json
import re
import sys

class HCP:
    def begin(self):
        self.line_1 = {}
        self.line_2={}
        self.list_1=[]
        self.list_2=[]
        self.url=“http://apis.baidu.com/qunar/qunar_train_service/s2ssearch?version=1.0&from=%s&to=%s&date=%s“%(self.ssself.ss_1self.ss_2)
        headers = {
            ‘apikey‘:‘361cf2a2459552575b0e86e0f62302bc‘
            }
        ww = requests.Session()
        ww.headers = headers
        json_1 = ww.get(self.url)
        json_2 =json.loads(json_1.text)[‘data‘][‘trainList‘]
        return json_2
    def solve(self):
        data = self.begin()
        if data is None:
            print(“没有获取到数据.“)
            sys.exit()
        
        #print(data)
       # print (length)
        for i in range(0length):
            self.list_1.append(data[i][‘startTime‘])
            self.list_1.append(data[i][‘from‘])
            self.list_1.append(data[i][‘seatInfos‘][0][‘seatPrice‘])#无座
            self.list_1.append(data[i][‘trainNo‘])
            self.list_1.append(data[i][‘duration‘])
            self.list_1.append(data[i][‘seatInfos‘][1][‘seatPrice‘])#硬座
            self.list_1.append(data[i][‘seatInfos‘][2][‘seatPrice‘])#硬卧
            self.list_1.append(data[i][‘seatInfos‘][3][‘seatPrice‘])#软卧
            self.line_1[i]=self.list_1
            self.list_1=[]
        for i in range(0length):
            self.list_2.append(data[i][‘endTime‘])
            self.list_2.append(data[i][‘to‘])
            self.line_2[i]=self.list_2
            self.list_2=[]             
        print(“以下为从去哪儿网得到的数据....“)
        print(‘车次/类型      发站/到站     发/到时间      运行时间   无座价钱   硬座价钱   硬卧价钱   软卧价钱  ‘)
        for i in self.line_1:
            for j in self.line_2:
                i_1 =int(i)
                j_1 =int(j)
                if i==j:
                  #  print(i)
                    print(self.line_1[i][3]“\t\t “self.line_1[i][1]“\t“self.line_1[i][0]“\t\t“self.line_1[i][4]“ “self.line_1[i][2]“  “self.line_1[i][5]“  “self.line_1[i][6]“   “self.line_1[i][7])
                    print(“\t\t“self.line_2[i][1]“\t“self.line_2[i][0])
                    print (“\n“)
          
        
    def start(self):
        print(“提示:输入日期的格式为(xxxx-xx-xx)比如(2016-12-01)退出为q“)
        while True:
            self.ss = input(‘输入你的起始地:‘)
            self.ss_1 = input(‘输入你的目的地:‘)
            if self.ss == ‘q‘ or self.ss_1 ==‘q‘:
                sys.exit()
            while True:
                self.ss_2 = input(‘输入日期:‘)
                tt = re.match(‘[0-9]{4}-[0-9]{2}-[0-9]{2}‘self.ss_2)
                if self.ss_2==‘q‘:
                    sys.exit()
                if tt is None:
                    print(“日期格式错误,请重新输入!“)
                    continue
                self.solve()
hcp = HCP()
hcp.start()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        3119  2016-11-30 15:02  houchepiao.py

评论

共有 条评论