我发现编写 Python 脚本以使用 Python 将广告发布到 Facebook 非常具有挑战性。特别是,我想发布出现在移动新闻提要中的广告。您能否提供样板代码来发布一个我可以扩展的广告?
我的目标是编写一个更完整的 Python 脚本,将数千条广告发布到 Facebook。广告的大部分内容都是相同的,但它们会根据性别、兴趣或设备以某种特定方式有所不同。
这已经晚了一年,但也许它会帮助通过谷歌或其他东西进来的人。:P
尝试 Facebook Python 广告 API SDK(以前不可用): https ://github.com/facebook/facebook-python-ads-sdk
那里有一些关于如何引导会话和创建对象的示例,但这里有一个快速片段:
from facebookads.session import FacebookSession
from facebookads.api import FacebookAdsApi
from facebookads.objects import AdGroup
my_app_id = '<APP_ID>'
my_app_secret = '<APP_SECRET>'
my_access_token = '<ACCESS_TOKEN>'
my_session = FacebookSession(my_app_id, my_app_secret, my_access_token)
my_api = FacebookAdsApi(my_session)
FacebookAdsApi.set_default_api(my_api)
new_adgroup = AdGroup(parent_id='act_<AD_ACCOUNT_ID>')
new_adgroup[AdGroup.Field.name] = 'My Adgroup'
new_adgroup[AdGroup.Field.campaign_id] = <AD_SET_ID>
new_adgroup[AdGroup.Field.creative] = {
'creative_id': <CREATIVE_ID>
}
new_adgroup.remote_create()
您使用我用 Python 编写的示例代码。根据您的目的并使用您的令牌、ID 等编辑代码。我测试过并且当前使用的是相同的脚本。
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.campaign import Campaign
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.targetingsearch import TargetingSearch
from facebook_business.adobjects.targeting import Targeting
import datetime
from facebook_business.adobjects.adset import AdSet
from facebook_business.adobjects.adimage import AdImage
access_token = ''
app_secret = ''
app_id = ''
ad_account_id = 'act_'
page_id = ''
FacebookAdsApi.init(access_token=access_token)
params = {
'name': 'ENTER CAMPAIGN NAME HERE',
'objective': 'POST_ENGAGEMENT',
'status': 'ACTIVE',
}
campaign_result = AdAccount(ad_account_id).create_campaign(params=params)
print(campaign_result)
today = datetime.date.today()
start_time = str(today)
end_time = str(today + datetime.timedelta(weeks=1))
adset = AdSet(parent_id=ad_account_id)
adset.update({
'name': 'ENTER ADSET NAME HERE',
'campaign_id': campaign_result["id"],
'daily_budget': 150,
'billing_event': 'IMPRESSIONS',
'optimization_goal': 'REACH',
'bid_amount': 10,
'targeting': {'geo_locations': {'countries': {'TR'}},
'publisher_platforms': 'facebook'},
'start_time': start_time,
'end_time': end_time,
})
adset.remote_create(params={'status': 'ACTIVE'})
print(adset)
image = AdImage(parent_id=ad_account_id)
image[AdImage.Field.filename] = 'ENTER AD IMAGE PATH HERE'
image.remote_create()
image_hash = image[AdImage.Field.hash]
print(image)
fields = [
]
params = {
'name': 'ENTER CREATIVE NAME HERE',
'object_story_spec': {'page_id':page_id,'link_data':{'image_hash':image_hash,'link':'ENTER FACEBOOK PAGE LINK-PAGE_ID HERE','message':'ENTER AD MESSAGE HERE'}},
}
adcreative = AdAccount(ad_account_id).create_ad_creative(fields=fields, params=params)
print(adcreative)
fields = [
]
params = {
'name': 'ENTER AD NAME HERE',
'adset_id': adset['id'],
'creative': {'creative_id': adcreative['creative_id']},
'status': 'ACTIVE'
}
print(AdAccount(ad_account_id).create_ad(fields=fields, params=params))
这个https://github.com/facebook/fbconsole/blob/master/src/fbconsole.py对我来说是一个很好的起点,因为我做了我需要用 Facebook 做的事情,尽管我没有用它做广告。