Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all users in a list Twitter API?

Is there a way to access all members in a list? Currently, I can only see the first 20 members? Specifically, I'm using python and tweepy.

like image 254
Parth Avatar asked Nov 09 '11 00:11

Parth


2 Answers

I do not know about tweepy. But twitter's REST API limits results. At the end of result it gives some kind of pointer to next page. Use that pointer :) https://dev.twitter.com/docs/api

like image 21
Melug Avatar answered Oct 15 '22 14:10

Melug


In Tweepy, this can be facilitated by using the Cursor class Tweepy provides, which implements an appropriate iterator for the model returned to you depending on your desired method call. In your case, you want to initialize a Cursor with the list_members() method and parameters for the list owner and the list slug (see https://dev.twitter.com/docs/api/1/get/lists/members).

Example (modified from http://packages.python.org/tweepy/html/code_snippet.html#pagination):

import tweepy
api = tweepy.API() # Don't forget to use authentication for private lists/users.

# Iterate through all members of the owner's list
for member in tweepy.Cursor(api.list_members, 'list_owner', 'slug').items():
    # Do something with member...

I wish I could link you some up-to-date documentation, but the only thing I can find is this version 1.4 documentation about using Cursors. This strategy is still the recommended way of doing pagination/iteration of results from the Twitter API resources in Tweepy.

like image 84
jmlane Avatar answered Oct 15 '22 16:10

jmlane