Google Analytics API分页循环未结束且未更新新请求Python中的pageToken值

2024-10-03 21:27:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)

尽管如此,它并没有按预期工作。我错过了什么?在


Tags: sampleclient参数datetimeresponserequestpaginationnow
2条回答

你需要做这样的事

### Something like this works for me

list = [] #I usually store the output of the pagination in a list

# pagination function
def main(client, pageTokenVariable):
    return analytics.reports().batchGet(
        body={
            'reportRequests': [
            {
            'viewId': '123',
            "pageToken": pageTokenVariable,
            #All your other stuff like dates etc goes here
            }]
        }
    ).execute()

response = main(client, "0")

for report in response.get(reports, []) #All the stuff you want to do
    pagetoken = report.get('nextPageToken', None) #Get your page token fron the FIRST request and store it a variabe
    columnHeader = report.get('columnHeader', {})
    dimensionHeaders = columnHeader.get('dimensions', [])
    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
    rows = report.get('data', {}).get('rows', [])
    for row in rows:
        # create dict for each row
        dict = {}
        dimensions = row.get('dimensions', [])
        dateRangeValues = row.get('metrics', [])

        # fill dict with dimension header (key) and dimension value (value)
        for header, dimension in zip(dimensionHeaders, dimensions):
          dict[header] = dimension

        # fill dict with metric header (key) and metric value (value)
        for i, values in enumerate(dateRangeValues):
          for metric, value in zip(metricHeaders, values.get('values')):
            #set int as int, float a float
            if ',' in value or ',' in value:
              dict[metric.get('name')] = float(value)
            else:
              dict[metric.get('name')] = int(value)
        list.append(dict) #Append that data to a list as a dictionary

    while pagetoken: #This says while there is info in the nextPageToken get the data, process it and add to the list
        response = main(client, pagetoken)
            for row in rows:
            # create dict for each row
            dict = {}
            dimensions = row.get('dimensions', [])
            dateRangeValues = row.get('metrics', [])

            # fill dict with dimension header (key) and dimension value (value)
            for header, dimension in zip(dimensionHeaders, dimensions):
            dict[header] = dimension

            # fill dict with metric header (key) and metric value (value)
            for i, values in enumerate(dateRangeValues):
            for metric, value in zip(metricHeaders, values.get('values')):
                #set int as int, float a float
                if ',' in value or ',' in value:
                dict[metric.get('name')] = float(value)
                else:
                dict[metric.get('name')] = int(value)
            list.append(dict) #Append that data to a list as a dictionary

#So to recap
#You make an initial call to your function passing a pagetoken to get it started.
#Get the nextPageToken), process the data and append to list
#If there is data in the nextPageToken call the function, process, add to list until nextPageToken is empty

我不知道这是否是一个可能的答案,但是您是否考虑过删除pageSize并添加{}parameter?在

此选项允许您最多查询10.000个元素,如果您有超过10.000个元素,则可以使用start-indexoption从10.000、20.000等处开始

因为在这个字段中总有多少个答案可以知道。在

相关问题 更多 >