返回 登录
0

通过Python调用天市数据的火车查询接口

前置条件
在开始前,请作如下准备
1.学会用Python输出“Hello World”
2.去 天市数据 申请火车查询专用的KEY

操作步骤
1.配置好Python开发环境
2.新建一个train.py文件,并输入以下内容:

#!/usr/bin/python
# encoding:utf-8
import urllib2, json, urllib, time 
# 1、站站查询 
data = {}
data["appkey"] = "输入你在天市数据申请的key"
data["start"] = raw_input("输入出发地:")
data["end"] = raw_input("输入出发地:")
data["ishigh"] = 0
url_values = urllib.urlencode(data)
url = "http://apis.tiisi.com/train/line" + "?" + url_values
request = urllib2.Request(url)
result = urllib2.urlopen(request)
jsonarr = json.loads(result.read())

if jsonarr["status"] != u"0":
    print jsonarr["msg"]
    exit()
result = jsonarr["result"]
for val in result:
    print val["trainno"],val["type"],val["station"],val["endstation"]

3.CMD下切换到train.py目录,输入python train.py,正常情况下你应该看到类似下面的内容:

{"status":"0","msg":"ok","result":[{"trainno":"G34","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"07:11","arrivaltime":"13:05","sequenceno":"1","costtime":"5时54分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"907.0","priceed":"301.0"},{"trainno":"G32","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"08:30","arrivaltime":"13:28","sequenceno":"1","costtime":"4时58分","distance":"1279","isend":"1","pricesw":"489.5","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"261.0","priceed":"155.0"},{"trainno":"G58","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"08:52","arrivaltime":"14:39","sequenceno":"4","costtime":"5时47分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G36","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"09:05","arrivaltime":"14:49","sequenceno":"1","costtime":"5时44分","distance":"1279","isend":"1","pricesw":"777.5","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"414.5","priceed":"246.0"},{"trainno":"G42","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"09:25","arrivaltime":"16:06","sequenceno":"1","costtime":"6时41分","distance":"1495","isend":"1","pricesw":"772.5","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"412.0","priceed":"244.5"},{"trainno":"G46","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"09:52","arrivaltime":"15:33","sequenceno":"6","costtime":"5时41分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G168","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"11:38","arrivaltime":"18:03","sequenceno":"8","costtime":"6时25分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G38","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"11:44","arrivaltime":"17:58","sequenceno":"9","costtime":"6时14分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G56","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"12:49","arrivaltime":"18:43","sequenceno":"10","costtime":"5时54分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"1025.5","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G164","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"13:41","arrivaltime":"19:49","sequenceno":"13","costtime":"6时8分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G40","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"15:18","arrivaltime":"20:56","sequenceno":"1","costtime":"5时38分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"907.0","priceed":"496.5"},{"trainno":"G166","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"15:23","arrivaltime":"21:34","sequenceno":"1","costtime":"6时11分","distance":"1279","isend":"1","pricesw":"951.5","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"507.0","priceed":"301.0"},{"trainno":"G60","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"15:44","arrivaltime":"21:29","sequenceno":"4","costtime":"5时45分","distance":"1279","isend":"1","pricesw":"1701","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"","pricerw2":"","priceyw1":"","priceyw2":"","priceyw3":"","priceyd":"907.0","priceed":"538.5"},{"trainno":"G44","type":"高铁","station":"杭州东","endstation":"北京南","departuretime":"16:15","arrivaltime":"23:01","sequenceno":"1","costtime":"6时46分","distance":"1477","isend":"1","pricesw":"1629","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"869.0","priceed":"516.0"},{"trainno":"Z10","type":"直达特快","station":"杭州","endstation":"北京","departuretime":"17:17","arrivaltime":"07:34","sequenceno":"1","costtime":"14时17分","distance":"1650","isend":"1","pricesw":"","pricetd":"","pricerz":"305","priceyz":"14.5","pricegr1":"","pricegr2":"","pricerw1":"38.5","pricerw2":"537","priceyw1":"23.5","priceyw2":"0","priceyw3":"351"},{"trainno":"T32","type":"特快","station":"杭州","endstation":"北京","departuretime":"18:20","arrivaltime":"10:26","sequenceno":"1","costtime":"16时6分","distance":"1650","isend":"1","pricesw":"","pricetd":"","pricerz":"305","priceyz":"23.5","pricegr1":"119.5","pricegr2":"","pricerw1":"64.5","pricerw2":"537","priceyw1":"39.5","priceyw2":"0","priceyw3":"351"},{"trainno":"K102","type":"快速","station":"杭州","endstation":"北京","departuretime":"19:24","arrivaltime":"15:43","sequenceno":"10","costtime":"20时19分","distance":"1591","isend":"1","pricesw":"","pricetd":"","pricerz":"","priceyz":"189.5","pricegr1":"","pricegr2":"","pricerw1":"504.5","pricerw2":"","priceyw1":"322.5","priceyw2":"","priceyw3":""}]}
评论