我正在使用 python 客户端库连接到 Google Ads 的 API。
ga_service = client_service.get_service('GoogleAdsService')
query = ('SELECT campaign.id, campaign.name, campaign.advertising_channel_type '
'FROM campaign WHERE date BETWEEN \''+fecha+'\' AND \''+fecha+'\'')
response = ga_service.search(<client_id>, query=query,page_size=1000)
result = {}
result['campanas'] = []
try:
for row in response:
print row
info = {}
info['id'] = row.campaign.id.value
info['name'] = row.campaign.name.value
info['type'] = row.campaign.advertising_channel_type
当我解析值时,这是我得到的结果:
{
"campanas": [
{
"id": <campaign_id>,
"name": "Lanzamiento SIKU",
"type": 2
},
{
"id": <campaign_id>,
"name": "lvl1 - website traffic",
"type": 2
},
{
"id": <campaign_id>,
"name": "Lvl 2 - display",
"type": 3
}
]
}
为什么我得到 result["type"] 的整数?当我检查回溯调用时,我可以看到一个字符串:
campaign {
resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
id {
value: 397083380
}
name {
value: "Lanzamiento SIKU"
}
advertising_channel_type: SEARCH
}
campaign {
resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
id {
value: 1590766475
}
name {
value: "lvl1 - website traffic"
}
advertising_channel_type: SEARCH
}
campaign {
resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
id {
value: 1590784940
}
name {
value: "Lvl 2 - display"
}
advertising_channel_type: DISPLAY
}
我在 Documentation for the API 上搜索过发现是因为字段:advertising_channel_type 是数据类型:枚举。如何操作 Enum 类的这个对象来获取字符串值?在他们的文档中没有关于此的有用信息。
请帮忙!!
最佳答案
Enum
带有一些在索引和字符串之间转换的方法
channel_types = client_service.get_type('AdvertisingChannelTypeEnum')
channel_types.AdvertisingChannelType.Value('SEARCH')
# => 2
channel_types.AdvertisingChannelType.Name(2)
# => 'SEARCH'
这是通过查看文档字符串发现的,例如
channel_types.AdvertisingChannelType.__doc__
# => 'A utility for finding the names of enum values.'
关于python - 如何操作 Google Ads API 的 Enum 类的对象 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53089024/