[python]代码库
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
from urllib import urlencode
#----------------------------------
# 足球联赛调用示例代码 - 聚合数据
# 在线接口文档:http://www.juhe.cn/docs/90
#----------------------------------
def main():
#配置您申请的APPKey
appkey = "*********************"
#1.足球联赛赛事查询
request1(appkey,"GET")
#2.球队赛事查询
request2(appkey,"GET")
#3.球队对战赛赛程查询
request3(appkey,"GET")
#足球联赛赛事查询
def request1(appkey, m="GET"):
url = "http://op.juhe.cn/onebox/football/league"
params = {
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
"league" : "", #联赛名称
}
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://op.juhe.cn/onebox/football/team"
params = {
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
"team" : "", #球队名称
}
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://op.juhe.cn/onebox/football/combat"
params = {
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
"hteam" : "", #主队球队名称
"vteam" : "", #客队球队名称
}
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()