广告集返回值不在参数字段中

时间:2018-06-22 22:58:19

标签: python facebook-graph-api facebook-ads-api facebook-sdk-3.0

我正在尝试使用python在Facebook商业SDK上获取我的企业的广告集。我想获取广告集,然后获取每个广告的见解。这会为广告集中的每个广告返​​回ad_id,但是当我传递参数时,它会为每个广告系列中的每个广告返​​回ad_id。当我使用相同的方法从任何广告中获取见解时,它将返回我增加的每个日期的见解。如何仅获取我在时间范围内指定的广告的ad_id?如果必须如此调用API,则无法继续调用它,因为它表示已达到极限。预先感谢。

def AdSet(campaign_id):
    from facebook_business.adobjects.campaign import Campaign
    from facebook_business.adobjects.adset import AdSet
    from facebook_business.adobjects.adaccount import AdAccount
    from facebook_business import FacebookSession
    from facebook_business import FacebookAdsApi
    from facebook_business.adobjects.adsinsights import AdsInsights
    from facebook_business.adobjects.ad import Ad
    from facebook_business.adobjects.adcreative import AdCreative
    import datetime
    import json
    import os
    import csv
    import time
    app_id = <>
    app_secret = <>
    access_token = <>
    #campaign_id = <>
    #campaign_id = 'act_' + campaign_id
    ### Setup session and api objects
    session = FacebookSession(
        app_id,
        app_secret,
        access_token,
    )
    api = FacebookAdsApi(session)
    FacebookAdsApi.set_default_api(api)
    campaign = Campaign(campaign_id)
    today = datetime.date.today()
    start_time = today - datetime.timedelta(weeks=1)
    end_time = today - datetime.timedelta(days=6)
    start_time_list = []
    end_time_list = []
    i=0
    while(i<7):
        string_start_time = str(start_time)
        string_end_time = str(end_time)
        start_time_list.append(string_start_time)
        end_time_list.append(string_end_time)
        params = {
            'time_range': {
                'since': string_start_time,
                'until': string_end_time,
                },
            'fields': [
                AdSet.Field.name,
                AdSet.Field.id,
                AdSet.Field.start_time
                ],
                }
        adsets = campaign.get_ad_sets(params=params)
        i+=1
        start_time += datetime.timedelta(days=1)
        end_time += datetime.timedelta(days=1)
    ad_id = []
    print(adsets)
    for adset in adsets:
        ad_id_dict = {}
        ad_id_dict['id']=adset['id']
        ad_id_dict['start_time'] = str(adset['start_time'])
        ad_id.append(ad_id_dict.copy())
    this_dir = os.path.dirname('/Users/brennanmanion/Desktop/Python/')
    accountTitlesFile = os.path.join(this_dir, 'accountTitles.json')
    accountTitles = open(accountTitlesFile)
    accounts = json.load(accountTitles)
    accountTitles.close()
    filename = ''
    for account in accounts:
        if(campaign_id==account):
            filename = accounts[campaign_id]
    with open(filename+" AdSets"+".csv", 'w') as csv_file:
        fieldnames = ad_id[0].keys()
        writer = csv.DictWriter(csv_file,fieldnames)
        writer.writeheader()
        writer.writerows(ad_id)
        csv_file.close()
AdSet(campaign_id = 'act_10152589240456003')

0 个答案:

没有答案