Google Ads API,他们的新版本的Google Adwords API以一种称为"GoogleAdsRow“的格式返回数据。我发现以这种格式呈现的信息根本没有任何用处,而且它非常令人困惑。我希望将文件打印为基本的.csv格式,但据我所知没有任何类型的分隔符。到目前为止,我只能将每一整行打印为一个单元格。没有帮助:-)。
我的main函数如下所示。我已经提供了两个使用注释块尝试的示例,其中标识了这两种尝试:
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发布于 2020-09-22 20:18:27
您需要分离出返回的各个行项,并将它们单独添加到CSV中。每列一个。
对于您的响应,您需要提取返回的google行的元素,并将它们放入CSV中的适当行。
row.campaign.value,
row.segments.date.value,
row.metrics.cost_micros.value / 1000000,
row.metrics.clicks.value etc.不幸的是,您不能直接从google行写入CSV,因为它是一个协议缓冲区,并且需要根据您认为合适的方式提取行的属性。
https://stackoverflow.com/questions/59778944
复制相似问题