Reputation: 53
I am using tweepy to query twitter followers. What I only want to keep is the earliest n followers. Due to twitter orders its followers in a reverse chronological order, what I can do now is to query all followers and store in a list, and then slice the last n items, which is quite inefficient. Does anyone has some ideas on it?
for page in tweepy.Cursor(api.followers, screen_name=specific_user).pages():
for follower in page:
# do something with follower
Upvotes: 2
Views: 590
Reputation: 7047
This question is old.. but here is a solution
Looking at GET followers/ids and GET followers/list we see that Twitter returns followers in the following manner:
At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues. Results are given in groups of 20 users and multiple “pages” of results can be navigated through using the next_cursor value in subsequent requests. See Using cursors to navigate collections for more information.
Looking at Tweepy Cursor Tutorial we see
Limits What if you only want n items or pages returned? You pass into the items() or pages() methods the limit you want to impose.
# Only iterate through the first 200 statuses
for status in tweepy.Cursor(api.user_timeline).items(200):
process_status(status)
# Only iterate through the first 3 pages
for page in tweepy.Cursor(api.user_timeline).pages(3):
process_page(page)
Or in this case you'd want something like this:
for user in tweepy.Cursor(constants.api.followers, screen_name="joerogan").items(200):
print(user)
Upvotes: 1