用Python/Djang显示Twitter关注者的关注者

2024-09-30 12:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的应用程序中建立了一个快速的部分,它可以查看用户的关注者,并突出显示用户关注的人(朋友)关注哪些人。在

我想知道两件事:

  1. 有没有更有效的方法?这似乎会超出Twitter的API限制,因为我需要检查每个用户朋友的好友。

  2. 这是在创建一个包含好友id和关注者的dict列表。相反,dict会更好地作为跟随者id,然后是跟随者的朋友。提示?

代码:

# Get followers and friends
followers = api.GetFollowerIDs()['ids']
friends = api.GetFriendIDs()['ids']

# Create list of followers user is not following
followers_not_friends = set(followers).difference(friends)

# Create list of which of user's followers are followed by which friends
followers_that_friends_follow = []
for f in friends:
    ff = api.GetFriendIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    followers_that_friends_follow.append({'friend': f, 'users': users })

Tags: of用户apiididscreatenot朋友
1条回答
网友
1楼 · 发布于 2024-09-30 12:19:08

关于你问题的第二部分:

import collections

followers_that_friends_follow = collections.defaultdict(list)
for f in friends:
    ff = api.GetFriendsIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    for user in users:
        followers_that_friends_follow[user].append(f)

这将产生一个字典:

keys=ids跟随用户、用户不跟随的关注者以及用户的朋友跟随的追随者。在

values=跟随跟随者的朋友的id列表,用户没有跟随

例如,如果用户的跟随者的id为23,并且用户的两个朋友(用户16和用户28)跟随用户23,那么使用键23应该得到以下结果

^{pr2}$

相关问题 更多 >

    热门问题