从页面名称获取公共Facebook页面列表的数字ID

时间:2018-03-28 12:59:32

标签: python-3.x facebook-graph-api

我有一个公共Facebook页面列表(例如媒体)我需要页面ID(数字),但只有名称。由于我有很多页面,我无法手动找到所有页面,因此我将如何手动处理?

举个例子,我的网页是: pages = ['financialtimes', 'dailytelegraph', 'theguardian'] 我想我需要做这样的事情:

url = "https://graph.facebook.com/v2.4/" + 'financialtimes' + '/?access_token=' + 'my_app_id' + 'my_app_secret'
print(url)

除了我需要很多页面。我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以设置一个功能并将列表中的项目传递给该功能,例如

# FB access
app_id = "my_app_id"
app_secret = "my_app_secret"
access_token = app_id + "|" + app_secret
#define function
def FacebookIDs(page_name, access_token=access_token):
    """ get page's numeric information """  
    # construct the URL string
    base = "https://graph.facebook.com/v2.4"
    node = "/" + str(page_name)
    parameters = "/?access_token=%s" % access_token
    url = base + node + parameters
    # retrieve data    
    with urllib.request.urlopen(url) as url:
        data = json.loads(url.read().decode()) 
    print(data)
    #return data # if you want the data not to be printed but to be the input for another function etc.
# define list
pages = ['financialtimes', 'dailytelegraph', 'theguardian']
# iterate over list
for page in pages: FacebookIDs(page, access_token)