首页 > 代码库 > Python 获取Facebook特定用户的粉丝数

Python 获取Facebook特定用户的粉丝数

CODE:

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-8-9
@author: guaguastd
@name: fan_pages_compare.py
'''

# impot login
from login import facebook_login

# import helper
from helper import pp, int_format

# access to facebook
facebook_api = facebook_login()

# find Pepsi and Coke in search results
print '---------------'
print 'Query for Pepsi by name'
print '---------------'
name = 'pepsi'
pp(facebook_api.request('search', {'q': name, 'type': 'page', 'limit':5}))
print

print '---------------'
print 'Query for Coke by name'
print '---------------'
name = 'coke'
pp(facebook_api.request('search', {'q': name, 'type': 'page', 'limit':5}))
print

# Use the ids to query for likes
pepsi_id = '56381779049' # Could also uee 'PepsiUS'
coke_id = '40796308305'  # Could also use 'CocaCola'

print '---------------'
print 'Query for likes'
print '---------------'
print "Pepsi likes:", int_format(facebook_api.get_object(pepsi_id)['likes'])
print "Coke likes:", int_format(facebook_api.get_object(coke_id)['likes'])

RESULT:

---------------
Query for Pepsi by name
---------------
{
 "paging": {
  "next": "https://graph.facebook.com/v1.0/search?limit=5&type=page&q=pepsi&access_token=...&offset=5&__after_id=enc_AeyEr2DOhYDDNGDRR0gUWr8xWN00Uq4BFCKMhCFNVtQU1hEBy8S1gbL7inKsbRqbIvo"
 }, 
 "data": [
  {
   "category": "Food/beverages", 
   "name": "Pepsi", 
   "id": "339150749455906"
  }, 
  {
   "category": "Food/beverages", 
   "name": "Pepsithai", 
   "id": "63619711274"
  }, 
  {
   "category": "Food/beverages", 
   "name": "PepsiCo", 
   "category_list": [
    {
     "id": "2252", 
     "name": "Food/Beverages"
    }
   ], 
   "id": "260431051694"
  }, 
  {
   "category": "Company", 
   "name": "Pepsi Center", 
   "category_list": [
    {
     "id": "211155112228091", 
     "name": "Event Venue"
    }, 
    {
     "id": "109976259083543", 
     "name": "Sports Venue & Stadium"
    }
   ], 
   "id": "111829892187838"
  }, 
  {
   "category": "Arts/entertainment/nightlife", 
   "name": "Pepsi Club", 
   "category_list": [
    {
     "id": "191478144212980", 
     "name": "Night Club"
    }
   ], 
   "id": "194989311723"
  }
 ]
}

---------------
Query for Coke by name
---------------
{
 "paging": {
  "next": "https://graph.facebook.com/v1.0/search?limit=5&type=page&q=coke&access_token=...&offset=5&__after_id=enc_AexTzaGHp5s3B1YHpjyu7izJPFN5xkZs5QC5d1kuDa1auOC5Sv7PD3Dk5dUTZNO0qzY"
 }, 
 "data": [
  {
   "category": "Food/beverages", 
   "name": "Coca-Cola", 
   "id": "40796308305"
  }, 
  {
   "category": "Tv show", 
   "name": "Coke Studio", 
   "category_list": [
    {
     "id": "133436743388217", 
     "name": "Arts & Entertainment"
    }
   ], 
   "id": "16944632531"
  }, 
  {
   "category": "Tv show", 
   "name": "Coke Studio at MTV", 
   "id": "127266094017030"
  }, 
  {
   "category": "Food/beverages", 
   "name": "Coca-Cola Zero", 
   "id": "61124008229"
  }, 
  {
   "category": "Food/beverages", 
   "name": "Diet Coke", 
   "id": "8605796091"
  }
 ]
}

---------------
Query for likes
---------------
Pepsi likes: 33,027,024
Coke likes: 87,244,352