您的位置:首页 > 编程语言 > Python开发

Python使用Ckan API查找并展示特定的resource内容

2016-07-29 11:29 465 查看
#-*- coding: utf-8 -*-
__author__ = 'LILI'

import requests
import json
import pprint

CKAN_URL = 'http://default.yourckan.com'
APIkey = 'API Key'
resourceID = 'resourceID'

def show_resource(APIkey, resourceID):
url = '{ckan}/api/action/resource_show'.format(
ckan=CKAN_URL)

headers = {}
headers['Content-Type'] = 'application/json'
headers['X-CKAN-API-Key'] = APIkey
headers['Authorization'] = APIkey
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'

data={}
data['id'] = resourceID
data = json.dumps(data).encode('ascii')

r = requests.post(url, headers=headers,data=data)
return r.status_code, r.text

if __name__ == '__main__':
status_code, text = show_resource(APIkey, resourceID)
pprint.pprint(status_code)
pprint.pprint(text)


将代码最前面的CKAN_URLAPIkeyresourceID分别置换为自己的CKAN网址、自己的API key和resource ID。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python api ckan