python地铁查询系统_基于Python的苏州实时公交/地铁接口调用代码实例

1.[代码][Python]代码

#!/usr/bin/python

# -*- coding: utf-8 -*-

import json, urllib

from urllib import urlencode

#----------------------------------

# 苏州实时公交/地铁调用示例代码 - 聚合数据

# 在线接口文档:http://www.juhe.cn/docs/31

#----------------------------------

def main():

#配置您申请的APPKey

appkey = "*********************"

#1.站台(编码)查询

request1(appkey,"GET")

#2.根据站台查询公交状态

request2(appkey,"GET")

#3.线路(编码)查询

request3(appkey,"GET")

#4.根据线路编码查询详细信息

request4(appkey,"GET")

#5.公交(路线)详情

request5(appkey,"GET")

#6.地铁始发站时刻

request6(appkey,"GET")

#7.地铁线路实时详细信息

request7(appkey,"GET")

#8.地铁站台列车到站时刻表

request8(appkey,"GET")

#站台(编码)查询

def request1(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/bus"

params = {

"station" : "", #需要查询的站台,如:纳米

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#根据站台查询公交状态

def request2(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/bus"

params = {

"stationCode" : "", #站台的编码,在查询站台时有返回

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#线路(编码)查询

def request3(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/bus"

params = {

"bus" : "", #线路名称或关键字,如11

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#根据线路编码查询详细信息

def request4(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/bus"

params = {

"busline" : "", #需要查询的线路编码,在查询线路时有返回

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#公交(路线)详情

def request5(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/info"

params = {

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#地铁始发站时刻

def request6(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/subwaytime"

params = {

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#地铁线路实时详细信息

def request7(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/subwayline"

params = {

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

"line_id" : "", #地铁线路编号

"lng" : "", #经度

"lat" : "", #纬度

"order" : "", #线路方向(参考值: 1-正方向,2-反方向,默认为1)

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

#地铁站台列车到站时刻表

def request8(appkey, m="GET"):

url = "http://apis.juhe.cn/szbusline/subwaystation"

params = {

"dtype" : "", #返回数据格式:json或xml,默认json

"key" : appkey, #你申请的key

"line_id" : "", #地铁线路编号

"station_id" : "", #地铁站台编号

"order" : "", #线路方向(参考值: 1-上行,2-下行,默认为1)

}

params = urlencode(params)

if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))

else:

f = urllib.urlopen(url, params)

content = f.read()

res = json.loads(content)

if res:

error_code = res["error_code"]

if error_code == 0:

#成功请求

print res["result"]

else:

print "%s:%s" % (res["error_code"],res["reason"])

else:

print "request api error"

if __name__ == '__main__':

main()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值