0

我正在尝试遵循 python networkx 教程,但它使用 python-twitter 库,并且与 tweepy 相比,该库在 twitter 速率限制方面表现不佳。我想知道如何在 tweepy 中做同样的事情;具体来说,我如何在 tweepy 中获取 getfriends API?这是使用python-twitter的代码:

from pylab import *
from networkx import *
import twitter
api = twitter.Api(consumer_key='consumer_key',
                      consumer_secret='consumer_secret',
                      access_token_key='access_token',
                      access_token_secret='access_token_secret')
friends = api.GetFriends()
G = XGraph()

for friend in friends:
    G.add_edge('myname',friend.name)

for friend in friends[-3:]:
    for user in api.GetFriends(friend.id):
            G.add_edge(friend.name,user.name)

我使用tweepy尝试过这样的事情:

auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth,wait_on_rate_limit=True)
Followers=[]
c=tweepy.Cursor(api.followers).items(100)
for user in c:
    print user.screen_name
    Followers.append(user.screen_name)

但现在我不知道如何继续翻译成 tweepy 来结交朋友的朋友,特别是python-twitter中的这一部分:

for friend in friends[-3:]:
    for user in api.GetFriends(friend.id):
            G.add_edge(friend.name,user.name)

请帮忙。

4

1 回答 1

1

您可以随时使用几种方便的方法来访问所需的信息。

API.friends_ids(id/screen_name/user_id)

此方法通常用于访问用户关注的人(由 any 定义id/screen_name/user_id),返回值是表示特定人的 id 的整数列表(唯一),现在如果您想提取关注的人您关注的特定用户,那么您可以使用以下内容:

people_followed_by_me = API.friends_ids('@Your_username123')
for person in people_followed_by_me:
    API.friends_ids(person)

API.followers_ids(id/screen_name/user_id)

此方法用于访问正在关注您的用户,您也可以通过在上述函数中传递他们的用户名/id 来提取其他人的关注者或朋友。

您可以随时参考此文档以消除进一步的疑问。

于 2015-02-10T10:52:27.107 回答