搜索
热搜: 活动 交友 discuz
Hi~登录注册
查看: 1901|回复: 1
打印 上一主题 下一主题

Python 获取Facebook用户的Friends的爱好

[复制链接]

44

主题

58

帖子

327

积分

实习版主

Rank: 7Rank: 7Rank: 7

积分
327
跳转到指定楼层
楼主
发表于 2018-10-22 16:28:21 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
CODE:

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

  3. '''
  4. Created on 2014-8-12
  5. @author: guaguastd
  6. @name: friends_popular_category.py
  7. '''

  8. # impot login
  9. from login import facebook_login

  10. # import helper
  11. #from helper import pp

  12. # calculating the most popular category among your friends
  13. from prettytable import PrettyTable
  14. from collections import Counter

  15. # access to facebook
  16. facebook_api = facebook_login()

  17. # get friends like through single request
  18. #friends_like = facebook_api.get_object('me', fields='id,name,friends.fields(id,name,likes)')
  19. #pp(friends_like)

  20. # get friends like through multi request
  21. friends = facebook_api.get_connections("me", "friends")['data']
  22. likes = { friend['name'] : facebook_api.get_connections(friend['id'], "likes")['data']
  23.          for friend in friends[:10]}
  24. #print 'likes:', likes

  25. friends_likes_categories = Counter([like['category']
  26.                         for friend in likes
  27.                           for like in likes[friend]])

  28. pt = PrettyTable(field_names = ['Category', 'Freq'])
  29. pt.align['Category'], pt.align['Freq'] = 'l', 'r'
  30. [ pt.add_row(flc) for flc in friends_likes_categories.most_common(10) ]
  31. print 'Top 10 likes categories for friends'
  32. print pt
复制代码




RESULT:

  1. Top 10 likes categories for friends
  2. +--------------------------+------+
  3. | Category                 | Freq |
  4. +--------------------------+------+
  5. | Community                |   47 |
  6. | Musician/band            |   34 |
  7. | Professional sports team |   10 |
  8. | Artist                   |    8 |
  9. | Public figure            |    5 |
  10. | Athlete                  |    5 |
  11. | Movie                    |    4 |
  12. | Local business           |    4 |
  13. | Actor/director           |    4 |
  14. | App page                 |    4 |
  15. +--------------------------+------+
复制代码



回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 立即注册

快速回复 返回顶部 返回列表