基于Python的中石化油价接口调用代码实例

2022-07-11 21:18:13   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《基于Python的中石化油价接口调用代码实例》,欢迎阅读!
中石化,油价,调用,实例,接口
基于Python的中石化油价接口调用代码实例

代码描述:基于Python的中石化油价接口调用代码实例 接口平台:聚合数据

#!/usr/bin/python

# -*- coding: utf-8 -*- import json, urllib

from urllib import urlencode



#---------------------------------- # 全国省市今日油价调用示例代码 聚合数据 # 在线接口文档:http://www.juhe.cn/docs/48 #----------------------------------



def main():



#配置您申请的APPKey

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



#1.全国省市今日油价

request1(appkey,"GET")



#全国省市今日油价

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

url = "http://apis.juhe.cn/cnoil/oil_city" params = {

"key" : appkey, #应用APPKEY(应用详细页查询)

"dtype" : "", #返回数据的格式,xmljson,默认json



}

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()


本文来源:https://www.wddqxz.cn/7ea8f362b0717fd5370cdc1f.html

相关推荐