7

I want to get a list of twitter followers/following of a particular user, when their screenname or user.id is specified. Can anyone please give the code snippet for it? Thanks.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Nazim Zeeshan
  • 713
  • 1
  • 12
  • 20
  • 5
    You need to show code that you've actually attempted a solution and failed, so we can address your issue properly. – onteria_ May 11 '11 at 11:56

3 Answers3

22

I'm the author of Twython. There's two different methods you can use for this; one that returns just follower IDs (get_followers_ids), and one that returns the statuses/etc of a follower set (get_followers_list).

Some example code for one would be like the following:

from twython import Twython

twitter = Twython()
followers = twitter.get_followers_ids(screen_name = "ryanmcgrath")

for follower_id in followers:
    print "User with ID %d is following ryanmcgrath" % follower_id

If you have IDs, you'd need to do further lookups yourself, so the latter method (get_followers_list) may be what you want. Keep in mind that Twython functions just mirror API key parameters from the official Twitter API docs, so the methods you can pass to an argument are the same as what you'll find on the docs.

Good luck!

Community
  • 1
  • 1
Ryan McGrath
  • 2,042
  • 14
  • 23
  • Seems this is not working anymore, something might have change in the TwitterAPI since then: print "User %d is following UserX" % follower_id TypeError: %d format: a number is required, not unicode – algarecu Apr 02 '15 at 14:27
  • This comment is intended to improve the answer given above. As mentioned in my suggestion, printing the list of follower ids only works if you follow the structure of the json returned by get_followers_ids(): {u'previous_cursor': 0, u'previous_cursor_str': u'0', u'next_cursor': xxxxxxxx, u'ids': [xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, ... for follower_id in followers['ids']: print "User %d is following the given user % follower_id That’s all folks. Thanks. – algarecu Apr 03 '15 at 08:20
2
from twython import Twython

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

followers = twitter.get_followers_ids(id = 1234) # or just () - followers for your account

print(twitter.get_followers_ids()['ids']) # ids list of followers
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Port
  • 86
  • 12
1

It should be :

followers = twitter.get_followers_list(screen_name = "whatever")
Taryn
  • 242,637
  • 56
  • 362
  • 405