Full list of twitter "friends" using python and tweepy

John picture John · Sep 20, 2014 · Viewed 12.9k times · Source

By friends I mean all of the twitter users who I am following.

Is it possible using tweepy with python 2.7.6 to display a full list of all friends?

I have found it possible to display a list which contains some of my friends with the following code. After handling authorization of course.

api = tweepy.API(auth)
user = api.get_user('MyTwitterHandle')
print "My Twitter Handle:" , user.screen_name
ct = 0

for friend in user.friends():
    print friend.screen_name
    ct = ct + 1


print "\n\nFinal Count:", ct

This code successfully prints what appears to be my 20 most recent friends on Twitter, the ct variable is equal to 20. This method excludes the rest of the users I am following on Twitter.

Is it possible to display all of the users I am following on twitter? Or at least a way to adjust a parameter to allow me to include more friends?

Answer

alecxe picture alecxe · Sep 20, 2014

According to the source code, friends() is referred to the GET friends / list twitter endpoint, which allows a count parameter to be passed in:

The number of users to return per page, up to a maximum of 200. Defaults to 20.

This would allow you to get 200 friends via friends().

Or, better approach would be to use a Cursor which is a paginated way to get all of the friends:

for friend in tweepy.Cursor(api.friends).items():
    # Process the friend here
    process_friend(friend)

See also: