1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 import json 4 from urllib.request import urlopen 5 from urllib.parse import urlencode 6 7 #---------------------------------- 8 # 12306火车票查询调用示例代码 - 聚合数据 9 # 在线接口文档:http://www.juhe.cn/docs/22 10 #---------------------------------- 11 12 #站到站查询(含票价) 13 def request1(appkey, start_station, end_station, m="GET"): 14 url = "http://apis.juhe.cn/train/s2swithprice" 15 params = { 16 "start" : start_station, #出发站 17 "end" : end_station, #终点站 18 "key" : appkey, #应用APPKEY(应用详细页查询) 19 "dtype" : "json", #返回数据的格式,xml或json,默认json 20 21 } 22 params = urlencode(params) 23 if m =="GET": 24 f = urlopen("%s?%s" % (url, params)) 25 else: 26 f = urlopen(url, params) 27 28 content = f.read() 29 30 res = json.loads(str(content, encoding='utf8')) 31 if res: 32 error_code = res["error_code"] 33 if error_code == 0: 34 #成功请求 35 print (res["result"]) 36 # for item in res["result"]["list"]: 37 # print(item["train_type"],"\t", item["end_station"]) 38 else: 39 print ("%s:%s" % (res["error_code"],res["reason"])) 40 else: 41 print ("request api error") 42 43 #12306订票②:车次票价查询 44 def request2(appkey, m="GET"): 45 url = "http://apis.juhe.cn/train/ticket.price.php" 46 params = { 47 "train_no" : "", #列次编号,对应12306订票①:查询车次中返回的train_no 48 "from_station_no" : "", #出发站序号,对应12306订票①:查询车次中返回的from_station_no 49 "to_station_no" : "", #出发站序号,对应12306订票①:查询车次中返回的to_station_no 50 "date" : "", #默认当天,格式:2014-12-25 51 "key" : appkey, #应用APPKEY(应用详细页查询) 52 53 } 54 params = urlencode(params) 55 if m =="GET": 56 f = urlopen("%s?%s" % (url, params)) 57 else: 58 f = urlopen(url, params) 59 60 content = f.read() 61 res = json.loads(str(content, encoding='utf8')) 62 if res: 63 error_code = res["error_code"] 64 if error_code == 0: 65 #成功请求 66 print(res["result"]) 67 else: 68 print("%s:%s" % (res["error_code"],res["reason"])) 69 else: 70 print("request api error") 71 72 #车次查询 73 def request3(appkey, m="GET"): 74 url = "http://apis.juhe.cn/train/s" 75 params = { 76 "name" : "", #车次名称,如:G4 77 "key" : appkey, #应用APPKEY(应用详细页查询) 78 "dtype" : "", #返回数据的格式,xml或json,默认json 79 80 } 81 params = urlencode(params) 82 if m =="GET": 83 f = urlopen("%s?%s" % (url, params)) 84 else: 85 f = urlopen(url, params) 86 87 content = f.read() 88 res = json.loads(str(content, encoding='utf8')) 89 if res: 90 error_code = res["error_code"] 91 if error_code == 0: 92 #成功请求 93 print(res["result"]) 94 else: 95 print("%s:%s" % (res["error_code"],res["reason"])) 96 else: 97 print("request api error") 98 99 #站到站查询 100 def request4(appkey, m="GET"): 101 url = "http://apis.juhe.cn/train/s2s" 102 params = { 103 "start" : "", #出发站 104 "end" : "", #终点站 105 "traintype" : "", #列车类型,G-高速动车 K-快速 T-空调特快 D-动车组 Z-直达特快 Q-其他 106 "key" : appkey, #应用APPKEY(应用详细页查询) 107 "dtype" : "", #返回数据的格式,xml或json,默认json 108 109 } 110 params = urlencode(params) 111 if m =="GET": 112 f = urlopen("%s?%s" % (url, params)) 113 else: 114 f = urlopen(url, params) 115 116 content = f.read() 117 res = json.loads(str(content, encoding='utf8')) 118 if res: 119 error_code = res["error_code"] 120 if error_code == 0: 121 #成功请求 122 print (res["result"]) 123 else: 124 print ("%s:%s" % (res["error_code"],res["reason"])) 125 else: 126 print ("request api error") 127 128 #12306实时余票查询 129 def request5(appkey, m="GET"): 130 url = "http://apis.juhe.cn/train/yp" 131 params = { 132 "key" : appkey, #应用APPKEY(应用详细页查询) 133 "dtype" : "", #返回数据的格式,xml或json,默认json 134 "from" : "", #出发站,如:上海虹桥 135 "to" : "", # 到达站,如:温州南 136 "date" : "", #出发日期,默认今日 137 "tt" : "", #车次类型,默认全部,如:G(高铁)、D(动车)、T(特快)、Z(直达)、K(快速)、Q(其他) 138 139 } 140 params = urlencode(params) 141 if m =="GET": 142 f = urlopen("%s?%s" % (url, params)) 143 else: 144 f = urlopen(url, params) 145 146 content = f.read() 147 res = json.loads(str(content, encoding='utf8')) 148 if res: 149 error_code = res["error_code"] 150 if error_code == 0: 151 #成功请求 152 print (res["result"]) 153 else: 154 print ("%s:%s" % (res["error_code"],res["reason"])) 155 else: 156 print ("request api error") 157 158 #12306订票①:查询车次 159 def request6(appkey, m="GET"): 160 url = "http://apis.juhe.cn/train/ticket.cc.php" 161 params = { 162 "from" : "", #出发站名称:如上海虹桥 163 "to" : "", #到达站名称:如温州南 164 "date" : "", #默认当天,格式:2014-07-11 165 "tt" : "", #车次类型,默认全部,如:G(高铁)、D(动车)、T(特快)、Z(直达)、K(快速)、Q(其他) 166 "key" : appkey, #应用APPKEY(应用详细页查询) 167 168 } 169 params = urlencode(params) 170 if m =="GET": 171 f = urlopen("%s?%s" % (url, params)) 172 else: 173 f = urlopen(url, params) 174 175 content = f.read() 176 res = json.loads(str(content, encoding='utf8')) 177 if res: 178 error_code = res["error_code"] 179 if error_code == 0: 180 #成功请求 181 print (res["result"]) 182 else: 183 print ("%s:%s" % (res["error_code"],res["reason"])) 184 else: 185 print ("request api error") 186 187 #火车票代售点查询 188 def request7(appkey, m="GET"): 189 url = "http://apis.juhe.cn/train/dsd" 190 params = { 191 "province" : "", #省份,如:浙江 192 "city" : "", #城市,如:温州 193 "county" : "", #区/县,如:鹿城区 194 "key" : appkey, #应用APPKEY(应用详细页查询) 195 "dtype" : "", #返回数据的格式,xml或json,默认json 196 197 } 198 params = urlencode(params) 199 if m =="GET": 200 f = urlopen("%s?%s" % (url, params)) 201 else: 202 f = urlopen(url, params) 203 204 content = f.read() 205 res = json.loads(str(content, encoding='utf8')) 206 if res: 207 error_code = res["error_code"] 208 if error_code == 0: 209 #成功请求 210 print (res["result"]) 211 else: 212 print ("%s:%s" % (res["error_code"],res["reason"])) 213 else: 214 print ("request api error") 215 216 #列车站点列表 217 def request8(appkey, m="GET"): 218 url = "http://apis.juhe.cn/train/station.list.php" 219 params = { 220 "key" : appkey, #应用APPKEY(应用详细页查询) 221 "dtype" : "", #返回数据的格式,xml或json,默认json 222 223 } 224 params = urlencode(params) 225 if m =="GET": 226 f = urlopen("%s?%s" % (url, params)) 227 else: 228 f = urlopen(url, params) 229 230 content = f.read() 231 res = json.loads(str(content, encoding='utf8')) 232 if res: 233 error_code = res["error_code"] 234 if error_code == 0: 235 #成功请求 236 print (res["result"]) 237 else: 238 print ("%s:%s" % (res["error_code"],res["reason"])) 239 else: 240 print ("request api error") 241 242 243 def main(): 244 245 #配置您申请的APPKey 246 appkey = "574d9d9f7a1f7c5f1e780e4d9f9268fe" 247 248 #1.站到站查询(含票价) 249 request1(appkey, start_station="西安", end_station="济南", m="GET") 250 251 # #2.12306订票②:车次票价查询 252 # request2(appkey,"GET") 253 254 # #3.车次查询 255 # request3(appkey,"GET") 256 257 # #4.站到站查询 258 # request4(appkey,"GET") 259 260 # #5.12306实时余票查询 261 # request5(appkey,"GET") 262 263 # #6.12306订票①:查询车次 264 # request6(appkey,"GET") 265 266 # #7.火车票代售点查询 267 # request7(appkey,"GET") 268 269 # #8.列车站点列表 270 # request8(appkey,"GET") 271 272 if __name__ == '__main__': 273 main()