TypeError:(''uUu init\\\至少接受3个参数(给定1个)',<class'GoogleAppClient.errors.HttpError'>, ())

2024-06-16 14:17:10 发布

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

我使用python的API从GA获取数据。这是我的代码:

    # -*- coding: utf-8 -*-
"""Hello Analytics Reporting API V4."""

import argparse
import pandas as pd
import time

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from pandas import Series, DataFrame
from multiprocessing import Pool
from functools import partial


SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
KEY_FILE_LOCATION = '/Users/mac/Documents/python-f/client_secrets.p12'
SERVICE_ACCOUNT_EMAIL = 'mac-test3@prime-imagery-150409.iam.gserviceaccount.com'
VIEW_ID = '108573919'
FILE_NAME = '/Users/mac/Documents/python-f/12ios_bbs_user.csv'
DATE_RANGE = pd.date_range('1/1/2016','12/11/2016')
FILTERS = 'ga:screenName=~论坛'
SEGMENTS_ID = 'gaid::gKXoOymDSxKWjmKVPpPsbA'


def initialize_analyticsreporting():
  """Initializes an analyticsreporting service object.

  Returns:
    analytics an authorized analyticsreporting service object.
  """

  credentials = ServiceAccountCredentials.from_p12_keyfile(
    SERVICE_ACCOUNT_EMAIL, KEY_FILE_LOCATION, scopes=SCOPES)

  http = credentials.authorize(httplib2.Http())

  # Build the service object.
  analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)

  return analytics


def get_report(q_date):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  analytics = initialize_analyticsreporting()
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': q_date, 'endDate': q_date}],
          'metrics': [{'expression': 'ga:screenviews'},{'expression': 'ga:users'}],
          'dimensions': [{'name': 'ga:date'},{'name': 'ga:segment'}],
          #'dimensions': [{'name': 'ga:date'}]
          'filtersExpression': FILTERS
          #'segments':  [{'segmentId': SEGMENTS_ID}]
        }]
      }
  ).execute()


def print_response(response):
  content=[]
  for report in response.get('reports', []):
    columnHeader = report.get('columnHeader', {})
    dimensionHeaders = columnHeader.get('dimensions', [])
    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
    rows = report.get('data', {}).get('rows', [])
    hed_r = []
    con_r = []

    mer_h = []
    for m in metricHeaders:
      mer_h.append(m.get('name'))

    hed_r = dimensionHeaders + mer_h

    con_r.append(hed_r)

    for row in rows:
      dimensions = row.get('dimensions', [])
      dateRangeValues = row.get('metrics', [])
      line=[]
      for di in dimensions:
        line.append(di)
      for values in dateRangeValues:
        for value in values.get('values'):
          line.append(value)
      con_r.append(line)
    content.append(con_r)
  df = DataFrame(content[0])
  df.columns = df.ix[0]

  return df[1:]


def main():

 #analytics1 = initialize_analyticsreporting()
 #startTime = time.time()
  results = []
  date_range = []

  for t in DATE_RANGE:
    date_range.append(t.strftime('%Y-%m-%d'))

  pool = Pool()
  rrr=pool.map(get_report, date_range)
  pool.close()
  pool.join()

  for rlt in rrr:
    rlt = print_response(rlt)
    results.append(rlt)

  results = pd.concat(results, ignore_index=True)
 #endTime = time.time()
  results.to_csv(FILE_NAME,encoding='utf-8')




  '''
  for i, t in enumerate(DATE_RANGE):
    rlt = get_report(analytics, t.strftime('%Y-%m-%d'))
    rlt_r = print_response(rlt)
    if i==0:
      results = rlt_r
    else:
      results = pd.concat([results, rlt_r])

    results.index = range(len(results))
    results.to_csv(FILE_NAME,encoding='utf-8')
'''

if __name__ == '__main__':
  main()

我第一次运行它,它就成功了。但是第二次,出现了一些错误,看起来是关于多线程的。错误消息:

^{pr2}$

我该怎么解决呢?在


Tags: infromimportreportforgetdatetime