遍历具有多个页面的API

时间:2019-03-02 12:01:36

标签: python json api

这是eventful.py文件。我使用它来搜索事件并将搜索结果存储在数据库中,但是我很难遍历所有页面,因为搜索结果限制为每页100个结果,而且我不知道如何获取所有结果结果。

"""
eventful

A Python interface to the Eventful API.

"""

__author__    = "Edward O'Connor <ted@eventful.com>"
__copyright__ = "Copyright 2005, 2006 Eventful Inc."
__license__   = "MIT"

import urllib

import httplib2
import simplejson

__all__ = ['APIError', 'API']

class APIError(Exception):
    pass

class API:
    def __init__(self, app_key, server='api.eventful.com', cache=None):
        """Create a new Eventful API client instance.
If you don't have an application key, you can request one:
    http://api.eventful.com/keys/"""
        self.app_key = app_key
        self.server = server
        self.http = httplib2.Http(cache)

    def call(self, method, **args):
        "Call the Eventful API's METHOD with ARGS."
        # Build up the request
        args['app_key'] = self.app_key
        args['page_size'] = 100
        args['page_number'] = 1




        if hasattr(self, 'user_key'):
            args['user'] = self.user
            args['user_key'] = self.user_key
        args = urllib.parse.urlencode(args)
        url = "http://%s/json/%s?%s" % (self.server, method, args)

        # Make the request
        response, content = self.http.request(url, "GET")

        # Handle the response
        status = int(response['status'])
        if status == 200:
            try:
                return simplejson.loads(content)
            except ValueError:
                raise APIError("Unable to parse API response!")
        elif status == 404:
            raise APIError("Method not found: %s" % method)
        else:
            raise APIError("Non-200 HTTP response status: %s" % response['status'])

如何浏览所有页码?当前,页码是在事件文件中通过args ['page_number'] = 1定义的,我想遍历所有页码吗?是否可以通过循环循环访问页码,直到page_number为false为止?

import eventful

api = eventful.API('KEY_HERE')

events = api.call('/events/search', q='', l='New York', c='music', t='This Week')


for event in events['events']['event']:
        print(event['image'])

0 个答案:

没有答案