我正在使用googleapiv4从googleanalytics下载一些数据。我正在获取数据,并尝试使用pageToken
参数在页面大小超过时请求下一页。但是,我的pagination函数应该将新的pageToken
传递到一个新请求中,它进入一个循环,在这个循环中它执行相同的第一个请求(假设这行:print(response['reports'][0]['nextPageToken'])
总是打印pagesize的最大值,这是nextPageToken
在第一个请求中获得的值)。在
查询应产生大约8000个结果/行。在
我试图为请求中的pageToken
参数创建一个变量,并使该变量获得递归函数发出的新请求中的nextPageToken
值:
pageTokenVariable = "whatever"
sample_request = {
'viewId': '1234',
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = 1),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
},
'dimensions': [
{'name': 'ga:date'},
{'name': 'ga:eventlabel'}
],
'metrics': [
{'expression': 'ga:users'},
{'expression': 'ga:totalevents'}
],
'pageToken':pageTokenVariable,
'pageSize': 1000
}
# pagination function
def main(client, pageTokenVariable):
response = client.reports().batchGet(
body={
'reportRequests':sample_request
}).execute()
if 'nextPageToken' in response['reports'][0]:
print(response['reports'][0]['nextPageToken']) #trying to debug
pageTokenVariable = response['reports'][0]['nextPageToken']
response = main(client, pageTokenVariable)
return(response)
尽管如此,它并没有按预期工作。我错过了什么?在
你需要做这样的事
我不知道这是否是一个可能的答案,但是您是否考虑过删除}parameter?在
pageSize
并添加{此选项允许您最多查询10.000个元素,如果您有超过10.000个元素,则可以使用
start-index
option从10.000、20.000等处开始因为在这个字段中总有多少个答案可以知道。在
相关问题 更多 >
编程相关推荐