您可以用Python演示如何将广告发布到Facebook的ads API吗? [英] Can you demonstrate in Python how to post an ad to Facebook's ads API?

查看:213
本文介绍了您可以用Python演示如何将广告发布到Facebook的ads API吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现编写Python脚本以Python将广告发布到Facebook非常具有挑战性.特别是,我想发布展示在移动新闻供稿中的广告.您可以提供一个模板代码来发布一个广告,我可以对其进行扩展吗?

I have found it very challenging to write a Python script to post ads to Facebook in Python. In particular, I want to post ads that show up in mobile news feeds. Can you provide boilerplate code to post one ad, that I can expand on?

我的目标是编写更完整的Python脚本,将成千上万的广告发布到Facebook.广告的大多数部分都是相同的,但是它们基于性别,兴趣或设备以某些特定方式有所不同.

My goal is to write a fuller Python script post thousands of ads to Facebook. Most parts of the ads are the same, but they vary base on gender, interest, or device in some specific way.

推荐答案

您使用我用Python编写的示例代码.根据您的目的以及您的令牌,ID等来编辑代码.我已经测试过并且当前使用的是同一脚本.

You use the example code i wrote with Python. Edit the code for your purpose and with your tokens, ids, etc. I tested and currently using this same script.

Github链接到此处的脚本!

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))

这篇关于您可以用Python演示如何将广告发布到Facebook的ads API吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆