使用nextLink属性获取下一个结果页面

Blu*_*ber 4 python google-analytics google-api

我正在使用Google API python客户端从Google Analytics下载一些数据.我基本上复制了他们的一个示例并修改它以完全按照我的需要进行操作.

我从示例中获取了这段代码:

   request = service.data().ga().get(
       ids=ids,
       start_date=str(start_date),
       end_date=str(end_date),
       dimensions=','.join(dimensions),
       filters=filters,
       sort="ga:date",
       metrics=','.join(metrics))
Run Code Online (Sandbox Code Playgroud)

然后将其添加到批处理对象,并在收集10个请求后执行它.这一切都运行良好,但问题是,其中一些请求返回"nextLink".现在我可以使用不同的start-index创建一个新的请求对象(带有上面的代码),但是不是更好的方法吗?

有没有办法将nextLink解析为新的请求对象?

小智 6

我正在使用这种方法:

firstRun = True
params = {'ids':'ga:00000001',
        'start_date':'2013-07-01',
        'end_date':'2013-07-31',
        'metrics':'ga:visits',
        'dimensions':'ga:source',
        'sort':'-ga:visits',
        'start_index':1,
        'max_results':10000}

while firstRun == True or result.get('nextLink'):
    if firstRun == False:
        params['start_index'] = int(params['start_index']) + int(params['max_results'])

    result = service.data().ga().get(**params).execute()
    firstRun = False
Run Code Online (Sandbox Code Playgroud)


小智 5

我找不到一种方法来解析nextLink对象并用它做一个请求,但这是我的解决方案并且工作正常:

max_results = 10000

params = {
    'ids': 'ga:' + profile_id,
    'start_date': start_date,
    'end_date': end_date,
    'metrics': ','.join(metrics),
    'dimensions': ','.join(dimensions),
    'start_index': 1,
    'max_results': max_results
}

has_more = True

while has_more:
    results = service.data().ga().get(**params).execute()

    #do something with results

    params['start_index'] = int(params['start_index']) + int(params['max_results'])
    has_more = results.get('nextLink')
Run Code Online (Sandbox Code Playgroud)