我想计算一个朋友喜欢使用Python的用户帖子的次数。
我已经成功获取了喜欢这些帖子的朋友的名字。但有一些帖子根本没有任何相似之处。代码在那里出错。我正在使用facebook-sdk 1.0.0。任何帮助将非常感激。 代码段是:
import facebook
graph=facebook.GraphAPI(access_token="")
posts=graph.get_connections(id="me",connection_name="posts")
for w in range (0,5):
p1=posts['data'][w]['likes']['data']
for i in range (0,len(p1)):
r=p1[i]['name']
print(r)
另外,有没有办法找出所有帖子中最喜欢的朋友列表?
答案 0 :(得分:0)
我对您在这里缺少的内容或您的问题有点不确定,但看起来您需要做的就是定义要查找的名称并计算代码中出现的次数。
这样的事情:
import facebook
likes = 0 # How many times they likes on your posts
friend = "Bob" # Your friends name
graph=facebook.GraphAPI(access_token="")
posts=graph.get_connections(id="me",connection_name="posts")
for w in range (0,5):
p1=posts['data'][w]['likes']['data']
for i in range (0,len(p1)):
if friend == p1[i]['name']
likes += 1
print likes
此外,如果您抛弃for循环的范围而不是迭代数据结构本身,则可以大大清理代码。像这样的东西会起作用:
import facebook
likes = 0 # How many times they likes on your posts
friend = "Bob" # Your friends name
graph=facebook.GraphAPI(access_token="")
posts=graph.get_connections(id="me",connection_name="posts")
for user_post in posts['data']:
for name in user_post['likes']['data']:
if friend == name['name']
likes += 1