如何使用python将API返回数据插入列表?

时间:2019-09-24 14:45:26

标签: python list facebook api facebook-graph-api

我正在使用facebook_business_api库进行呼叫以获取我的广告系列数据。 该调用正在运行,它返回带有字典的列表。 如何仅从返回的数据中的字典中获取数据? 感谢您的帮助!

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adaccountuser import AdAccountUser
from facebook_business.adobjects.campaign import Campaign as AdCampaign
from facebook_business.adobjects.adsinsights import AdsInsights

app_id = 'app_id'
app_secret = 'app_secret'
access_token = 'access_token'

FacebookAdsApi.init(app_id, app_secret, access_token)

me = AdAccountUser(fbid='me')
my_accounts = list(me.get_ad_accounts())

params={'date_preset':'today', 'level': 'campaign', 'limit': '20000'}


fields = [AdsInsights.Field.account_id,
           AdsInsights.Field.account_name,
           AdsInsights.Field.ad_id,
           AdsInsights.Field.ad_name,
           AdsInsights.Field.adset_id,
           AdsInsights.Field.adset_name,
           AdsInsights.Field.campaign_id,
           AdsInsights.Field.campaign_name,
           AdsInsights.Field.spend,
           AdsInsights.Field.impressions,
           AdsInsights.Field.clicks,
           AdsInsights.Field.outbound_clicks,
        ]

我输入了“ a”,在其中输入了“信息”列表中的所有数据,但是我不能仅访问每个词典的值,而是将所有信息相加。就像列表列表一样:

infos = []
for account in my_accounts:
    infos.append((
            account.get_insights(params=params, fields=fields)
    ))
print(infos)

返回:

<AdsInsights> {
"account_id": "0000000",
"account_name": "AAAAA",
"campaign_id": "AAAAAA",
"campaign_name": "AAAAAA",
"clicks": "0000",
"date_start": "2019-09-24",
"date_stop": "2019-09-24",
"impressions": "0000",
"outbound_clicks": [
    {
        "action_type": "outbound_click",
        "value": "0000"
    }
],
    "spend": "00000"
}, <AdsInsights> {
    "account_id": "00000",
    "account_name": "AAAAA",
"campaign_id": "000000",
"campaign_name": "AAAAA",
"clicks": "0000",
"date_start": "2019-09-24",
"date_stop": "2019-09-24",
"impressions": "11111",
"outbound_clicks": [
    {
        "action_type": "outbound_click",
        "value": "0000"
    }
],
"spend": "000.00"
}], []]

0 个答案:

没有答案