Facebook 营销 API Python 中的 CSV 广告洞察
Facebook Marketing API Ads Insights to CSV in Python
如何将 Facebook 营销 API 广告洞察力的响应导出到 CSV。
下面是我的代码及其响应。
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
app_id = 'xxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxx'
access_token = 'xxxxxxxxx'
FacebookAdsApi.init(app_id, app_secret, access_token)
params = {'time_range': {'since': '2020-01-01', 'until': '2020-12-25'},
'time_increment':1,
'level': 'adset',
'sort': ['spend_descending'],
'export_format':'csv'}
fields = ['account_name',
'campaign_name',
'campaign_id',
'adset_name',
'adset_id',
'impressions',
'clicks',
'cpm',
'spend',
'ctr']
insights = AdAccount('act_332828570147114').get_insights(params=params, fields=fields)
print(insights)
响应是
[<AdsInsights> {
"account_name": "xxx",
"adset_id": "xxx",
"adset_name": "xxx",
"campaign_id": "xxx",
"campaign_name": "xxx",
"clicks": "xxx",
"cpm": "xxx",
"ctr": "xxx",
"date_start": "xxx",
"date_stop": "xxx",
"impressions": "xxx",
"spend": "xxx"
}, <AdsInsights> {
"account_name": "xxx",
"adset_id": "xxx",
"adset_name": "xxx",
"campaign_id": "xxx",
"campaign_name": "xxx",
"clicks": "xxx",
"cpm": "xxx",
"ctr": "xxx",
"date_start": "xxx",
"date_stop": "xxx",
"impressions": "xxx",
"spend": "xxx"
}]
我们如何将此回复导出为 CSV 格式?任何建议将不胜感激。
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
app_id = 'xxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxx'
access_token = 'xxxxxxxxx'
FacebookAdsApi.init(app_id, app_secret, access_token)
params = {'time_range': {'since': '2020-01-01', 'until': '2020-12-25'},
'time_increment':1,
'level': 'adset',
'sort': ['spend_descending'],
'export_format':'csv'}
fields = ['account_name',
'campaign_name',
'campaign_id',
'adset_name',
'adset_id',
'impressions',
'clicks',
'cpm',
'spend',
'ctr']
insights = list(AdAccount('act_332828570147114').get_insights(params=params, fields=fields))
import pandas as pd
df=pd.DataFrame(columns=fields)
for field in fields:
df["{}".format(field)]=[x['{}'.format(field)] for x in insights]
df.to_csv("insights.csv",index=False)
将 adinsights 对象作为字典进行交互,并在保存为 csv 之前将它们添加到数据框中!
如何将 Facebook 营销 API 广告洞察力的响应导出到 CSV。
下面是我的代码及其响应。
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
app_id = 'xxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxx'
access_token = 'xxxxxxxxx'
FacebookAdsApi.init(app_id, app_secret, access_token)
params = {'time_range': {'since': '2020-01-01', 'until': '2020-12-25'},
'time_increment':1,
'level': 'adset',
'sort': ['spend_descending'],
'export_format':'csv'}
fields = ['account_name',
'campaign_name',
'campaign_id',
'adset_name',
'adset_id',
'impressions',
'clicks',
'cpm',
'spend',
'ctr']
insights = AdAccount('act_332828570147114').get_insights(params=params, fields=fields)
print(insights)
响应是
[<AdsInsights> {
"account_name": "xxx",
"adset_id": "xxx",
"adset_name": "xxx",
"campaign_id": "xxx",
"campaign_name": "xxx",
"clicks": "xxx",
"cpm": "xxx",
"ctr": "xxx",
"date_start": "xxx",
"date_stop": "xxx",
"impressions": "xxx",
"spend": "xxx"
}, <AdsInsights> {
"account_name": "xxx",
"adset_id": "xxx",
"adset_name": "xxx",
"campaign_id": "xxx",
"campaign_name": "xxx",
"clicks": "xxx",
"cpm": "xxx",
"ctr": "xxx",
"date_start": "xxx",
"date_stop": "xxx",
"impressions": "xxx",
"spend": "xxx"
}]
我们如何将此回复导出为 CSV 格式?任何建议将不胜感激。
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
app_id = 'xxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxx'
access_token = 'xxxxxxxxx'
FacebookAdsApi.init(app_id, app_secret, access_token)
params = {'time_range': {'since': '2020-01-01', 'until': '2020-12-25'},
'time_increment':1,
'level': 'adset',
'sort': ['spend_descending'],
'export_format':'csv'}
fields = ['account_name',
'campaign_name',
'campaign_id',
'adset_name',
'adset_id',
'impressions',
'clicks',
'cpm',
'spend',
'ctr']
insights = list(AdAccount('act_332828570147114').get_insights(params=params, fields=fields))
import pandas as pd
df=pd.DataFrame(columns=fields)
for field in fields:
df["{}".format(field)]=[x['{}'.format(field)] for x in insights]
df.to_csv("insights.csv",index=False)
将 adinsights 对象作为字典进行交互,并在保存为 csv 之前将它们添加到数据框中!