Google Ads API,其新版本的Google Adwords API以称为“ GoogleAdsRow”的格式返回数据。对于以这种格式显示的信息,我根本没有发现任何用处,这非常令人困惑。我想将文件打印为基本的.csv格式,但据我所知,没有任何类型的定界符。到目前为止,我只能将每一行打印为单个单元格。没有帮助:-)。
我的主要功能如下。我提供了两个示例,其中我用注释框标识了两种尝试:
def main(client, customer_id, page_size):
ga_service = client.get_service('GoogleAdsService', version='v2')
query = ('SELECT ad_group.id, ad_group_criterion.type, '
'ad_group_criterion.criterion_id, '
'ad_group_criterion.keyword.text, '
'ad_group_criterion.keyword.match_type FROM ad_group_criterion '
'WHERE ad_group_criterion.type = KEYWORD')
results = ga_service.search(customer_id, query=query, page_size=page_size)
try:
with open(path, "w", encoding = "utf-8", newline = "") as f:
#with open(path, "w") as csv:
csv_writer = csv.writer(f, delimiter=',')
for row in results:
campaign = row.campaign
csv_writer.writerow([row]) #Prints entire returned row as a single cell
csv_writer.writerow(row) #Tells me there is no delimiter
可迭代的错误如下
<ipython-input-15-e736ee2d05c9> in main(client, customer_id, page_size)
17 campaign = row.campaign
18 #csv_writer.writerow([row]) #Prints entire returned row as a single cell
---> 19 csv_writer.writerow(row) #Tells me there is no delimiter
20
21
Error: iterable expected, not GoogleAdsRow
您需要分离出返回的各个行项目,并将其分别添加到CSV中。每列一个。
对于您的回复,您需要提取返回的google行的元素,并将其放入CSV中的相应行。
row.campaign.value,
row.segments.date.value,
row.metrics.cost_micros.value / 1000000,
row.metrics.clicks.value etc.
不幸的是,您不能直接从google行中写入CSV,因为它是probbuff,因此您需要按需删除该行的属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句