我如何使用Facebook-business-python-sdk从Facebook的见解api中获得超过25个结果?

时间:2019-07-03 07:31:47

标签: facebook-marketing-api facebook-insights facebook-python-business-sdk

我正在尝试使用facebook-python-business-sdk和python 3.7从Facebook的见解API获取基本的广告级别数据。 问题是,即使在拥有超过25个有效广告的帐户中,我也只能获得25个结果。

我在每个帐户上使用get_insights方法,传递一个'level':'ad'参数并在特定日期进行过滤。 我还检查了我是否已经达到了Facebook的限制(使用here提供的说明),我甚至还没有达到限制。 get_insights方法没有“限制”参数,而且无论哪种方式,我都不想限制它,因为某些帐户可能有数百甚至数千个广告。

这是我正在使用的代码

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adaccountuser import AdAccountUser
from facebook_business.adobjects.campaign import Campaign as AdCampaign
from facebook_business.adobjects.adsinsights import AdsInsights

access_token = '******'
app_secret = '******'
app_id = '******'

FacebookAdsApi.init(app_id, app_secret, access_token)

me = AdAccountUser(fbid='me')
my_accounts = list(me.get_ad_accounts())

params={'time_range': {'since': '2019-06-29', 'until': '2019-06-29'},'level': 'ad'}
fields = [AdsInsights.Field.account_id,
           AdsInsights.Field.account_name,
           AdsInsights.Field.ad_id,
           AdsInsights.Field.ad_name,
           AdsInsights.Field.adset_id,
           AdsInsights.Field.adset_name,
           AdsInsights.Field.campaign_id,
           AdsInsights.Field.campaign_name,
           AdsInsights.Field.spend,
           AdsInsights.Field.impressions,
           AdsInsights.Field.clicks,
           AdsInsights.Field.outbound_clicks,
        ]

for account in my_accounts:
    ads = account.get_insights(params=params, fields=fields)
    print(ads)
    print(len(ads))

我希望每个帐户都能获得所有广告,但每个帐户最多只能获得25个广告。 任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:1)

解决方案只是添加一个“限制”参数,请参见下文:

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adaccountuser import AdAccountUser
from facebook_business.adobjects.campaign import Campaign as AdCampaign
from facebook_business.adobjects.adsinsights import AdsInsights

access_token = '******'
app_secret = '******'
app_id = '******'

FacebookAdsApi.init(app_id, app_secret, access_token)

me = AdAccountUser(fbid='me')
my_accounts = list(me.get_ad_accounts())

params={'time_range': {'since': '2019-06-29', 'until': '2019-06-29'},'level': 'ad', 'limit': '20000'}
fields = [AdsInsights.Field.account_id,
           AdsInsights.Field.account_name,
           AdsInsights.Field.ad_id,
           AdsInsights.Field.ad_name,
           AdsInsights.Field.adset_id,
           AdsInsights.Field.adset_name,
           AdsInsights.Field.campaign_id,
           AdsInsights.Field.campaign_name,
           AdsInsights.Field.spend,
           AdsInsights.Field.impressions,
           AdsInsights.Field.clicks,
           AdsInsights.Field.outbound_clicks,
        ]

for account in my_accounts:
    ads = account.get_insights(params=params, fields=fields)
    print(ads)
    print(len(ads))

感谢@Matteo