pandas 如何拉年龄,性别,位置,设备从facebook的一天使用python

s5a0g9ez  于 2023-03-11  发布在  Python
关注(0)|答案(1)|浏览(91)

我尝试用python从Facebook广告中提取年龄、性别、位置和设备。到目前为止,我只找到了年龄和性别,没有位置和设备。而且,我运行的代码有语法错误。
下面是我的代码:

from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adsinsights import AdsInsights
from facebook_business.api import FacebookAdsApi
from facebookads.adobjects.adset import AdSet
from facebookads.adobjects.targeting import Targeting
import pandas as pd

access_token = 'access token'
ad_account_id = 'act_123456789'
app_secret = '123456789'
app_id = '123456789'
FacebookAdsApi.init(access_token=access_token, api_version = 'v15.0')
my_account = AdAccount('act_123456789')
campaigns = my_account.get_campaigns()
# Define the API parameters
fields = [
    'campaign_name',
    'adset_name',
    'ad_name',
    'attribution_setting',
    'reach',
    'impressions',
    'frequency',
    'spend',
    'clicks',
    'inline_link_clicks',
    'objective',
    'cpm',
    'date_start',
    'date_stop',
]
params = {
    'time_increment': {'1'},
    'filtering': [],
    'level': 'ad',
    'breakdowns': ['age', 'gender'],
    'date_preset': 'last_90d'
}

# Initialize the Facebook Ads API client
FacebookAdsApi.init(access_token=access_token, api_version='v15.0')

# Retrieve insights for the ad account
ads_insights = AdAccount(ad_account_id).get_insights(fields=fields, params=params)

# Convert the results to a Pandas DataFrame
fb_ad = pd.DataFrame(ads_insights)

# Print the DataFrame
print(fb_ad)

我得到的错误如下:

File "D:\Anaconda\lib\site-packages\facebookads\adobjects\adset.py", line 565
    def get_insights(self, fields=None, params=None, async=False, batch=None, pending=False):
                                                     ^
SyntaxError: invalid syntax

我是新的API和没有真正理解的API文件从Facebook。任何帮助将不胜感激,谢谢。
如果您有任何问题,请告诉我,谢谢。

f4t66c6m

f4t66c6m1#

答案是简单地删除这两个软件包。

from facebookads.adobjects.adset import AdSet
from facebookads.adobjects.targeting import Targeting

其余的软件包仍然可以完成这项工作。

相关问题