11

Using tweepy I am able to return all of my friends using a cursor. Is it possible to specify another user and get all of their friends?

user = api.get_user('myTwitter')
print "Retreiving friends for", user.screen_name
for friend in tweepy.Cursor(api.friends).items():
 print "\n", friend.screen_name

Which prints a list of all my friends, however if I change the first line to another twitter user it still returns my friends. How can I get friends for any given user using tweepy?

#first line is changed to
user = api.get_user('otherUsername') #still returns my friends

Additionally user.screen_name when printed WILL return otherUsername

The question Get All Follower IDs in Twitter by Tweepy does essentially what I am looking for however it returns only a count of ID's. If I remove the len() function I will I can iterate through a list of user IDs, but is it possible to get screen names @twitter,@stackoverflow, @etc.....?

Community
  • 1
  • 1
user2497792
  • 505
  • 2
  • 9
  • 18
  • possible duplicate of [Get All Follower IDs in Twitter by Tweepy](http://stackoverflow.com/questions/17431807/get-all-follower-ids-in-twitter-by-tweepy). The variable `ids` in the accepted answer contains all IDs, the answerer prints its length but you can do whatever you want with it. – Luigi Nov 08 '14 at 18:04
  • I don't necessarily believe this to be duplicate question, I am looking for the twitter handles or `screen_name`, not IDs which is what is returned in the question I included it in the question itself as reference since my code is essentially the same – user2497792 Nov 09 '14 at 23:56

2 Answers2

17

You can use the ids variable from the answer you referenced in the other answer to get the the id of the followers of a given person, and extend it to get the screen names of all of the followers using Tweepy's api.lookup_users method:

import time
import tweepy

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)

ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name="McDonalds").pages():
    ids.extend(page)
    time.sleep(60)

screen_names = [user.screen_name for user in api.lookup_users(user_ids=ids)]
Community
  • 1
  • 1
Luigi
  • 4,129
  • 6
  • 37
  • 57
0

You can use this:

# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# the screen_name of the targeted user
screen_name = "TwitterIndia"
  
# printing the latest 20 friends of the user
for friend in api.friends(screen_name):
    print(friend.screen_name)

for more details see https://www.geeksforgeeks.org/python-api-friends-in-tweepy/

Ali Hosein pour
  • 220
  • 4
  • 19