gpt4 book ai didi

python - 将 Google Analytics API 转换为 CSV

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:18 26 4
gpt4 key购买 nike

我正在尝试为我的 Google Analytics(分析)帐户构建一个 API,以将数据导出到 CSV 文件中。我有身份验证代码,但我现在正在努力以我想要的格式打印数据。

我暂时只拉维度country,维度city,metric session。 (但是当我开始工作时这些会改变。)现在,它打印:

Date Range(0)
ga:sessions: 2
ga:country:United States
ga:city:Los Angeles
...

但是,我想把它放在一行中:

date Range   sessions    country     city
0 2 USA Los Angeles
...

我需要使用 Python 中的哪些代码?以下是我所拥有的。

def initialize_analyticsreporting():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[tools.argparser])
flags = parser.parse_args([])

http = credentials.authorize(httplib2.Http())
service = build('analytics', 'v4', http=http, discoveryServiceUrl=('https://analyticsreporting.googleapis.com/$discovery/rest'))


def get_report(service):
return service.reports().batchGet(
body={
'reportRequests':[
{
"viewId": "ga:52783868",
"dimensions": [{
"name": "ga:country"},
{"name": "ga:city"}],

"metrics": [{
"expression": "ga:sessions"}],
"dateRanges": [{
"startDate": "2017-04-10",
"endDate": "2017-04-12"}]
}
]
}
).execute()

countries = []
cities = []
val = []

def print_reponse(response):

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

for row in rows:
dimensions = row.get('dimensions',[])
dateRangeValues=row.get('metrics',[])

for header, dimension in zip(dimensionHeaders,dimensions):
print(header+':'+dimension)

for i, values in enumerate(dateRangeValues):
for metricHeader, value in zip(metricHeaders, values.get('values')):
print(metricHeader.get('name')+':'+value)

def main():
analytics = initialize_analyticsreporting()
response = get_report(service)
print_reponse(response)



if __name__ == '__main__':
main()

最佳答案

根据lrnzcig的建议,我们可以用pandas解析数据,然后导出到csv文件。

首先,导入pandasjson_normalize

import pandas as pd
from pandas.io.json import json_normalize

使用该函数解析数据

def parse_data(response):

reports = response['reports'][0]
columnHeader = reports['columnHeader']['dimensions']
metricHeader = reports['columnHeader']['metricHeader']['metricHeaderEntries']

columns = columnHeader
for metric in metricHeader:
columns.append(metric['name'])

data = json_normalize(reports['data']['rows'])
data_dimensions = pd.DataFrame(data['dimensions'].tolist())
data_metrics = pd.DataFrame(data['metrics'].tolist())
data_metrics = data_metrics.applymap(lambda x: x['values'])
data_metrics = pd.DataFrame(data_metrics[0].tolist())
result = pd.concat([data_dimensions, data_metrics], axis=1, ignore_index=True)

return result

结果看起来像...

      ga:country         ga:city    ga:sessions
0 (not set) (not set) 64
1 Argentina (not set) 1
2 Australia Adelaide 3
3 Australia Brisbane 9
...

最后调用to_csv函数将数据保存为csv文件

result.to_csv('result.csv')

关于python - 将 Google Analytics API 转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43477702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com