def main(client, adgroup_id):
# Initialize appropriate service.
ad_group_criterion_service = client.GetService(
'AdGroupCriterionService', version='v201802')
# Construct selector and get all ad group criteria.
offset = 0
selector = {
'fields': ['Id', 'CriteriaType', 'KeywordMatchType', 'KeywordText','FinalUrls'],
'predicates': [
{
'field': 'AdGroupId',
'operator': 'EQUALS',
'values': [adgroup_id]
},
{
'field': 'CriteriaType',
'operator': 'EQUALS',
'values': ['KEYWORD'],
}
],
'paging': {
'startIndex': str(offset),
'numberResults': str(PAGE_SIZE)
},
'ordering': [{'field': 'KeywordText', 'sortOrder': 'ASCENDING'}]
}
more_pages = True
operations = []
while more_pages:
page = ad_group_criterion_service.get(selector)
# Display results.
if 'entries' in page:
for keyword in page['entries']:
print ('Keyword ID "%s", type "%s", text "%s", and match type '
'"%s" was found.' % (
keyword['criterion']['id'],
keyword['criterion']['type'],
keyword['criterion']['text'],
keyword['criterion']['matchType']))
pprint(keyword)
trgt={
'criterion':keyword['criterion'],
}
operations.append({'operator': 'SET', 'operand': trgt })
else:
print ('No keywords were found.')
offset += PAGE_SIZE
selector['paging']['startIndex'] = str(offset)
more_pages = offset < int(page['totalNumEntries'])
ad_group_criteria = ad_group_criterion_service.mutate(operations)['value']
if __name__ == '__main__':
# Initialize client object.
CLIENT_ID = '####'
CLIENT_SECRET = '####'
REFRESH_TOKEN = '###'
# AdWords API information.
DEVELOPER_TOKEN = '###'
USER_AGENT = '####'
CLIENT_CUSTOMER_ID = '###'
oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT,client_customer_id=CLIENT_CUSTOMER_ID)
adgroupsList=[{'id':54627912232,'nh':'','category':''},{'id':57139439947,'nh':'','category':''},{'id':60015897731,'nh':'','category':''}]
for adgroup in adgroupsList:
main(adwords_client, adgroup["id"])