![](/img/trans.png)
[英]Facebook Marketing API - how to handle rate limit for retrieving *all* ad sets through campaign ids?
[英]Overcoming Rate Limiting in Facebook Marketing API
具體來說,我正在嘗試做一些與這個問題非常相似的事情(同樣的問題): FB Ads API (#17) User request limit reached
但是,我正在嘗試在 python 中執行此操作(並且 API 自 15 年以來已經發生了很大變化)。 這是我的代碼(即使有睡眠時間,它也會把我踢出去)——我想知道是否有人可以幫助我調用具有類似信息的數組,以減少我的總調用次數。
my_account = AdAccount(ad_account_id)
camps = my_account.get_campaigns(fields=[Campaign.Field.name])
for campaign in camps[0:100]:
time.sleep(5)
print campaign[Campaign.Field.name]
adsets = campaign.get_ad_sets([AdSet.Field.name, AdSet.Field.status])
for adset in adsets:
print '\t', adset[AdSet.Field.name]
for stat in adset.get_insights(fields=[
'impressions',
'clicks',
'spend',
'unique_clicks',
]):
for statfield in stat:
print "\t\t%s:\t%s" % (statfield, stat[statfield])
更一般地說,我打算如何在這個限制內為我的需求(大規模變更)編碼? 實際上,我想通過 go 編寫一個代碼,並在我公司的每個廣告集中更改一些選項(例如,“Expand Interests when...”從關閉到打開)。 我們有數百個廣告集,API 文檔說更改比創建消耗的調用多 10-100 倍(我都沒有堅持,只是閱讀。),這僅僅是一個問題嗎? 在每次更改之間讓代碼休眠 60 秒,他們不太清楚您在一段時間內接到了多少電話。 或者檢查這些呼叫的時間段有多寬,例如,如果它是每日限制。 那么睡覺不會幫助我改變 1200 個廣告集的選項。
我確實看到了有關升級的文檔 ( https://developers.facebook.com/docs/marketing-api/access ),但是在審查過程中,一切都基於公共(面向客戶,多用戶)應用程序。 我想要做的就是能夠從僅限桌面開發人員的內部腳本進行調用以進行批量更改。 我找錯地方了嗎?
將此添加到您的代碼中,您將永遠不必擔心 FB 的速率限制。 一旦接近限制,您的腳本將自動休眠,然后在冷卻后從它離開的地方開始。 享受 :)
import logging
import requests as rq
#Function to find the string between two strings or characters
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
#Function to check how close you are to the FB Rate Limit
def check_limit():
check=rq.get('https://graph.facebook.com/v3.3/act_'+account_number+'/insights?access_token='+my_access_token)
call=float(find_between(check.headers['x-business-use-case-usage'],'call_count":','}'))
cpu=float(find_between(check.headers['x-business-use-case-usage'],'total_cputime":','}'))
total=float(find_between(check.headers['x-business-use-case-usage'],'total_time":',','))
usage=max(call,cpu,total)
return usage
#Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your loop, every 200-500 iterations)
if (check_limit()>75):
print('75% Rate Limit Reached. Cooling Time 5 Minutes.')
logging.debug('75% Rate Limit Reached. Cooling Time 5 Minutes.')
time.sleep(300)
如果你現在只是在讀取數據,為什么不做一個批量請求呢? 我和你做的一樣,但最終請求更多的數據(我不得不擺弄它,因為數據太多,FB也不允許),然后循環數據。
出於我的目的,如果達到我的限制,我會批量異步請求 + 睡眠(10 秒)。 對我來說效果很好。
它可能對誰有用,我通過捕獲每個 API 調用后返回的“標頭”解決了這個問題。
my_account = AdAccount(ad_account_id)
camps = my_account.get_campaigns(fields=[Campaign.Field.name])
for campaign in camps[0:100]:
time.sleep(5)
print campaign[Campaign.Field.name]
adsets = campaign.get_ad_sets([AdSet.Field.name, AdSet.Field.status])
for adset in adsets:
print '\t', adset[AdSet.Field.name]
adset_insights = adset.get_insights(fields=[
'impressions',
'clicks',
'spend',
'unique_clicks',
])
for stat in adset_insights:
for statfield in stat:
print "\t\t%s:\t%s" % (statfield, stat[statfield])
rate_limit = json.loads(adset_insights.headers()[
'x-fb-ads-insights-throttle'])
if rate_limit['acc_id_util_pct'] > your_percentage_limit:
time.sleep(150)
json.loads()
因為有些值被當作文本處理,所以為了將其轉換為可下標的對象。
除了Smith Orlando's answer之外,還有其他參數,例如:
total_time 和 total_cputime 限制為 100。
acc_id_util_pct 的最大值是 100,因為它也是一個百分比,但是即使您達到了該限制,您也可以發出更多請求,因為您還沒有達到 total_time 和 total_cputime 限制。
對於每種類型的請求,max call_count 的計算方式都不同。 您可以從鏈接中了解更多信息
上述解決方案的問題在於,它們需要每秒向 Facebook API 發出特定請求以檢查限制,這將速率降低了一半(因為每隔一個請求用於檢查限制)。 針對這個問題,我提出了兩種解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.