欢迎来到天天文库
浏览记录
ID:8827634
大小:83.50 KB
页数:2页
时间:2018-04-08
《基于python的长途汽车时刻表接口调用代码实例》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、基于Python的长途汽车时刻表接口调用代码实例代码描述:基于Python的长途汽车时刻表接口调用代码实例代码平台:聚合数据#!/usr/bin/python#-*-coding:utf-8-*-importjson,urllibfromurllibimporturlencode #----------------------------------#长途汽车信息调用示例代码-聚合数据#在线接口文档:http://www.juhe.cn/docs/82#---------------------------------- defmain(): #配置您申请的APPKey
2、appkey="*********************" #1.汽车站信息查询 request1(appkey,"GET") #2.汽车站到站检索 request2(appkey,"GET") #汽车站信息查询defrequest1(appkey,m="GET"): url="http://op.juhe.cn/onebox/bus/query" params={ "station":"",#城市名称,如:北京 "key":appkey,#应用APPKEY(应用详细页查询) } params=ur
3、lencode(params) ifm=="GET": f=urllib.urlopen("%s?%s"%(url,params)) else: f=urllib.urlopen(url,params) content=f.read() res=json.loads(content) ifres: error_code=res["error_code"] iferror_code==0: #成功请求 printres["result"] el
4、se: print"%s:%s"%(res["error_code"],res["reason"]) else: print"requestapierror" #汽车站到站检索defrequest2(appkey,m="GET"): url="http://op.juhe.cn/onebox/bus/query_ab" params={ "from":"",#出发城市,如:上海 "to":"",#到达城市,如:苏州 "key":appkey,#应用APPKEY(应用详细页查询) }
5、 params=urlencode(params) ifm=="GET": f=urllib.urlopen("%s?%s"%(url,params)) else: f=urllib.urlopen(url,params) content=f.read() res=json.loads(content) ifres: error_code=res["error_code"] iferror_code==0: #成功请求 printres["result
6、"] else: print"%s:%s"%(res["error_code"],res["reason"]) else: print"requestapierror" if__name__=='__main__': main()
此文档下载收益归作者所有