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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coffee_cream/article/details/52063065
#-*- 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。

猜你喜欢

转载自blog.csdn.net/coffee_cream/article/details/52063065